您可以在没有 UI 的情况下使用 Activity。
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());