安卓状态栏通知: make it un-clearable and have it return to app (not start a new instance)

标签 android notifications statusbar

我正在开发一个 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/

相关文章:

android - Python SL4A 开发

android - 在 teamcity Android 中使用 keystore 生成已发布的 apk

android - Flutter - 无法在android设备中运行

javascript - 移动浏览器上的固定比例元素

ios - 在 iOS 的自定义抽屉导航中状态栏颜色更改为白色?

IOS 7 获取错误的状态栏高度

android - Cordova Android 不会显示 StatusBar

Android Sqlite 数据库架构

ios - iPad - MPMoviePlayer 缩小后消失

android - 访问 Android NotificationListenerService 设置