从通知栏恢复我的Android应用程序(并且应用程序图标仍显示在栏中)

时间:2016-10-26 10:14:12

标签: android notifications pause

我想恢复我的应用,就在我离开通知图标的位置。然后我使用了这段代码:

    Intent resultIntent = new Intent(this, MainActivity.class);

    resultIntent.setAction(Intent.ACTION_MAIN);
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_speed)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.txt_welcome))
            .setAutoCancel(true).setDefaults(Notification.DEFAULT_LIGHTS).setWhen(System.currentTimeMillis())
            .setOngoing(true);
    builder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, builder.build());

效果很好,问题是当我按下通知栏中的图标时,它会消失,我需要它留在那里。

当然,如果我删除这三行:

resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
manifest.xml中的

<activity android:name=".MainActivity"
          android:launchMode="singleInstance"
          android:screenOrientation="portrait"/>

点击后图标不会消失,但我总是会重定向到MainActivity,而不是我离开的地方(当应用暂停时)。

任何想法?

4 个答案:

答案 0 :(得分:1)

尝试设置:

setAutoCancel(false)

参考:https://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)

希望它有所帮助!

修改

根据讨论,需要在清单中设置:

android:launchMode="singleTop" 

这将为“onNewIntent”功能提供新意图,可用于在您的活动已经运行时执行进一步操作。

答案 1 :(得分:0)

通知应该在进行

mBuilder.setOngoing(true); //this will make ongoing notification

答案 2 :(得分:0)

请参考以下示例,如果不能正常工作,请告诉我

 private static final int NOTIFICATION_ID = 1;
 private NotificationManager mNotificationManager = null;
 private NotificationCompat.Builder mNotificationBuilder;

 if (mNotificationManager == null) {
            mNotificationManager = (android.app.NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 }
 if (mNotificationBuilder == null) {
            mNotificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.drawable.app_icon_96x96)
                    .setContentText(getContentString(false, 0))
                    .setAutoCancel(false)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setOngoing(true)
                    .setCategory(NOTIFICATION_SERVICE);
 }

答案 3 :(得分:0)

试试这段代码..!

void notifyme(String string){

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)
                                            getSystemService(ns);

int icon = R.drawable.notification_icon;        // icon from resources
CharSequence tickerText = string + " Program Running...";     // ticker-text
long when = System.currentTimeMillis();         // notification time
Context context = getApplicationContext();      // application Context
CharSequence contentTitle = *********;  // expanded message title
CharSequence contentText = string + " Program Running...";//expanded msg text

Intent notificationIntent = new Intent(this, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(
                                            this, 0, notificationIntent, 0);

// the next two lines initialize the Notification, using the configurations
// above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText,
                                                            contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}

添加此

final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);