我正在开发一个 Android 应用程序,我想要一个不能被用户清除的状态栏通知。有谁知道这是怎么做到的吗?我在 Skimble 等应用程序中看到过它们,在使用该应用程序时会出现不可清除的通知。
此外,当用户单击/按下通知时,我希望它返回到已在运行的应用程序实例。现在它启动了一个新实例。再次与 Skimble 应用程序一样,我只想返回到已运行的应用程序实例。
谢谢。
最佳答案
我找到了解决这个问题的方法。我使用 FLAG_ONGOING_EVENT 使通知持续存在。这是代码:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.mypic;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags = Notification.FLAG_ONGOING_EVENT;
Context context = getApplicationContext();
CharSequence contentTitle = "Title Text";
CharSequence contentText = "Content text.";
Intent intent = new Intent(this, MyClass.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
关于安卓状态栏通知: make it un-clearable and have it return to app (not start a new instance),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6866265/