Android 状态栏通知:使其不可清除并返回到应用程序(不启动新实例)

2024-03-23

我正在开发一个 Android 应用程序,我想要一个用户无法清除的状态栏通知。有谁知道这是怎么做到的吗?我在 Skimble 等应用程序中见过它们,在使用该应用程序时会出现不可清除的通知。

另外,当用户单击/按下通知时,我希望它返回到已经运行的应用程序实例。现在它启动一个新实例。再次像 Skimble 应用程序一样,我只想返回到该应用程序已经运行的实例。

Thanks.


我找到了解决这个问题的方法。我使用 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 状态栏通知:使其不可清除并返回到应用程序(不启动新实例) 的相关文章

随机推荐