如何在不启动整个应用程序的情况下更新自定义通知?

How to update Custom notification without bringing up the entire Application?

到目前为止,我已经能够在我的播放器开始播放时创建自定义通知,但问题是每当我单击通知按钮时,它不仅会停止媒体播放器按钮,还会将按钮更改为停止图标。

但要实现这一点,我必须将整个应用程序从死里复活,当用户在通知中单击停止按钮时,整个应用程序刚刚重新出现,这有点烦人。

我正在使用 intent 调用 MainActivty 以停止播放器并更新播放器已停止的通知栏。

似乎只有正常的通知示例,例如显示简单信息和单击按钮关闭通知。拉我的头发 5 天,有人可以帮我解决这个问题吗?请?

PS: 我不想使用 MediaStyle 通知。

您可以使用 Activity 而不使用 UI。

public class NotificationActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     //do your stuff
    finish();//end activity
}

public static PendingIntent getViewIntent(int id){
    Intent intent = new Intent(AppContext.context, NotificationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);      
    return PendingIntent.getActivity(AppContext.context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
 }
}

在您的清单中:

<activity
        android:name=".NotificationActivity"
        android:taskAffinity=""
        android:excludeFromRecents="true"
        android:theme="@android:style/Theme.NoDisplay">
    </activity>

在 NotificationBuilder 中

 NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(AppContext.context);

 PendingIntent resultPendingIntent = NotificationActivity.getViewIntent(id);
    mBuilder.setContentIntent(resultPendingIntent);

要使用新图标更新您的通知,您必须使用您想要的图标重新创建自定义通知,并使用相同的通知 ID 再次显示通知。

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    int notificationID =100;// this ID has to be same as your previuos ID
    // mId allows you to update the notification later on.
    mNotificationManager.notify(notificationID, mBuilder.build());