Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在后台使用通知将应用程序带到前台(模拟相同的主屏幕图标行为)_Android_Android Notifications - Fatal编程技术网

Android 在后台使用通知将应用程序带到前台(模拟相同的主屏幕图标行为)

Android 在后台使用通知将应用程序带到前台(模拟相同的主屏幕图标行为),android,android-notifications,Android,Android Notifications,我知道还有其他类似的问题,我都试过了,但都没有成功。这就是为什么我要在这里发布我的代码,如果有人能够为我的案例可视化适当的解决方案,并在代码中建议具体的操作,请提供帮助 我尝试过:在intent调用的活动中向manifest文件添加一些标记,在代码中向实际intent添加标志、操作和类别,创建一个虚拟活动,以便在intent中使用finish()调用,等等 谢谢你的建议 NotificationCompat.Builder mBuilder = new Notification

我知道还有其他类似的问题,我都试过了,但都没有成功。这就是为什么我要在这里发布我的代码,如果有人能够为我的案例可视化适当的解决方案,并在代码中建议具体的操作,请提供帮助

我尝试过:在intent调用的活动中向manifest文件添加一些标记,在代码中向实际intent添加标志、操作和类别,创建一个虚拟活动,以便在intent中使用finish()调用,等等

谢谢你的建议

NotificationCompat.Builder mBuilder =
         new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(getString(R.string.app_name))
        .setStyle(new NotificationCompat.BigTextStyle().bigText(getString(R.string.lampp)))
        .setAutoCancel(true)
        .setContentText(getString(R.string.lampp));

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

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build()); 
多亏了@Merlevede发布的答案和之前的一篇文章,这就是问题的解决方式:

将此代码用于通知:

NotificationCompat.Builder mBuilder =
         new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(getString(R.string.app_name))
        .setStyle(new NotificationCompat.BigTextStyle().bigText(getString(R.string.lampp)))
        .setAutoCancel(false)
        .setContentText(getString(R.string.lampp));

Intent resultIntent = new Intent(this, NotiActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build()); 
这是在intent NotiActivity.class中调用的虚拟活动的代码:

public class NotiActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    finish();

} 
  } 

希望这能帮助其他人

我不知道这对你是否有用。您可能缺少通知意图中的一些标志,特别是查看
标志活动\u清除\u顶部
标志活动\u单个\u顶部

我对使用此代码的服务使用通知

Intent notificationIntent = new Intent(this, ActivityMain.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent)
您的代码有一些不同之处。另外,我正在使用来自服务的此通知,因此我正在使用
startForeground
设置通知


值得一试。

您使用TaskStackBuilder有什么原因吗?您可以不用吗?嗨,Merlevede,我正在使用基本开发人员示例。。这是唯一的原因。。。我可以没有。。。是的,顺便说一下,你还没有描述你的问题。它现在根本不工作,还是正在创建另一个活动实例?它正在启动或重新启动应用程序,作为一个新实例。我想做的是:你在应用程序中->按下主页按钮->你在主屏幕中->按下应用程序图标->你在应用程序中,就在你离开它的地方,即使是你写的同样的文本也没有保存它。。。