从通知栏恢复我的 android 应用程序(应用程序图标仍显示在通知栏中)
Resume my android app from Notification bar (and app icon still shows in bar)
我想恢复我的应用程序,就在我离开栏中通知图标的位置。然后我使用了这段代码:
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,而不是我离开的地方(当应用程序进入暂停时)。
有什么想法吗?
通知应该持续
mBuilder.setOngoing(true); //this will make ongoing notification
请参考下面的示例,如果它不起作用请告诉我
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);
}
尝试设置:
setAutoCancel(false)
参考:https://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)
希望对您有所帮助!
编辑:
根据讨论,需要在清单中设置:
android:launchMode="singleTop"
这将为 "onNewIntent" 函数提供新的意图,如果您的 activity 已经 运行。
,该函数可用于执行进一步的操作
试试这个代码..!
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);
我想恢复我的应用程序,就在我离开栏中通知图标的位置。然后我使用了这段代码:
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,而不是我离开的地方(当应用程序进入暂停时)。
有什么想法吗?
通知应该持续
mBuilder.setOngoing(true); //this will make ongoing notification
请参考下面的示例,如果它不起作用请告诉我
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);
}
尝试设置:
setAutoCancel(false)
参考:https://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)
希望对您有所帮助!
编辑:
根据讨论,需要在清单中设置:
android:launchMode="singleTop"
这将为 "onNewIntent" 函数提供新的意图,如果您的 activity 已经 运行。
,该函数可用于执行进一步的操作试试这个代码..!
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);