Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
Java Android通知是否打开当前正在运行的应用程序而不是创建新进程?_Java_Android_Android Notifications_Deep Linking_Uri Scheme - Fatal编程技术网

Java Android通知是否打开当前正在运行的应用程序而不是创建新进程?

Java Android通知是否打开当前正在运行的应用程序而不是创建新进程?,java,android,android-notifications,deep-linking,uri-scheme,Java,Android,Android Notifications,Deep Linking,Uri Scheme,Im接收到带有自定义Uri方案的推送通知:myapp://main 在我的onReceive中,我创建了一个通知: public void createNotification(Context context, String title, String message, String summary, Uri uri) { Notification.Builder notification = new Notification.Builder(context) .

Im接收到带有自定义Uri方案的推送通知:
myapp://main

在我的
onReceive
中,我创建了一个通知:

public void createNotification(Context context, String title, String message, String summary, Uri uri) {
    Notification.Builder notification = new Notification.Builder(context)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.drawable.ic_launcher)
            .setDefaults(Notification.DEFAULT_LIGHTS)
            .setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && summary != null)
        notification.setSubText(summary);


    Intent intent = new Intent("android.intent.action.VIEW", uri);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    notification.setContentIntent(pendingIntent);

    Notification noti;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        noti = notification.build();
    else
        noti = notification.getNotification();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, noti);
}
当我点击该通知时,它会打开一个新的活动,这是我的主要活动。但它似乎也创建了一个全新的流程,而不仅仅是打开我当前运行的应用程序


是否缺少一些标记?

您可以如下设置意图,意图操作和类别与您在AndroidManifest.xml中指定的匹配

 Intent intent = new Intent()  
            .setAction(Intent.ACTION_VIEW)  
            .addCategory(Intent.CATEGORY_DEFAULT)  
            .addCategory(Intent.CATEGORY_BROWSABLE)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP) 
            .setPackage(getPackageName())
            .setData(uri);

活动有启动模式

android:launchMode=["multiple" | "singleTop" |"singleTask" | "singleInstance"]
了解有关启动模式的更多信息

这可以放在
清单
中的活动标签中


要获取意图数据,请尝试
onNewIntent()。如果它被系统以某种方式杀死,那么它将创建一个新的进程。您还可以在清单中将活动模式设置为
单任务
,这样它将始终像singleton@MurtazaKhursheedHussain我该怎么做?对不起,我是新来安卓的。在你的
@MurtazaKhursheedHussain中,这是有效的!现在我只是想知道如何从Uri中获取参数?因为onCreate不会再被调用了。这很有效!现在我只是想知道如何从Uri中获取参数?因为onCreate将不再被调用。如果此活动实例存在,android系统将调用onNewIntent(),否则将调用onCreate()