首页 文章

深层链接和多个应用实例

提问于
浏览
38

我在我的应用中实现了深层链接 . 我在清单文件中添加了此intent过滤器,并且深层链接正在运行 .

<intent-filter>
    <action android:name="android.intent.action.VIEW" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <category android:name="android.intent.category.BROWSABLE" /> 
    <category android:name="android.intent.category.VIEW" /> 
    <data
        android:host="www.mywebsite.com"
        android:pathPrefix="/something"
        android:scheme="http" />
</intent-filter>

问题是通过深度链接,我的应用程序将在当前应用程序之上启动 . 如果我在Gmail中并点击了一个链接,那么我的应用就会在Gmail上启动 . 我想以不同方式启动我的应用程序 .

如果我的应用已经在后台运行,我点击Gmail中的一个重定向到我的应用的链接,我将同时运行两个应用实例;一个在后台,另一个在Gmail之上 . 我想一次只运行我的应用程序的一个实例,因此它不在当前应用程序(Gmail)之上 . 我怎样才能做到这一点?

6 回答

  • 2

    接受的答案对我不起作用,这是做了什么:

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();
    

    来自官方文件:

    如果已设置,并且正在启动的活动已在当前任务中运行,则不会启动该活动的新实例,而是将关闭其上的所有其他活动,并将此Intent传递给(现在在顶部)旧活动作为一个新的意图 .

  • 9

    你需要为你的Manifest中的Activity做一些事情 .

    android:launchMode="singleTask"
    

    这告诉系统始终启动Activity的现有实例(如果已创建) .

    然后你可以通过覆盖方法来处理Intent

    onNewIntent
    

    有关更多信息,请参阅http://developer.android.com/guide/topics/manifest/activity-element.html .

  • 1

    我有这个完全相同的问题,除了我希望用户使用完整的后台堆栈返回主任务,好像他们刚刚使用应用程序切换器移动到我的应用程序 . 要做到这一点,我不得不重新安排任务 .

    1)授予我的应用程序重新排序任务的权限

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.company.app">
         <uses-permission android:name="android.permission.REORDER_TASKS"/>
    </manifest>
    

    2)跟踪主要任务ID是什么

    public class MainActivity {
        public static int mainTaskId;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
             super(savedInstanceState);
             //set the main task ID
             taskId = getTaskId();
        } 
    }
    

    3)当我的深层链接活动启动时,它会保存一些数据供以后使用,然后将主要任务带到前面

    public class DeepLinkActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super(savedInstanceState);
    
            //persist deep link data
            Uri uri = intent.getData();
            String action = intent.getAction();
            saveForLater(uri, action);
    
            if(isTaskRoot()){
                //I'm in my own task and not the main task
                final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                activityManager.moveTaskToFront(MainActivity.taskId, ActivityManager.MOVE_TASK_NO_USER_ACTION);
                }
            }
        }
    }
    

    4)当主任务的后端堆栈顶部的任何活动开始时,它会检查是否有任何已保存的数据要处理,并对其进行处理 .

  • 0

    我们在深层链接方面遇到了几个问题 . 喜欢:

    • 只点击两次相同的链接,首先点击触发正确的视图

    • 打开了多个实例

    • whatsapp或facebook中的链接因为他们的网络浏览器而在whatsapp本身打开了一个视图 .
      _69_在android 6上只打开了一个实例,但只处理第一个意图,第二个和第三个打开应用但没有动作,因为intentdata没有以某种方式改变 .

    因此,对于我们所遇到的几个问题,以下问题不是一个明确的答案 .

    Our solution:

    a)创建了一个新的FragmentActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2); //well can be anything some "loading screen"
    
        Intent intent = getIntent();
        String intentUrl = intent.getDataString();
        Intent newIntent = new Intent(this, MainActivity.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        newIntent.putExtra("intentUrl",intentUrl);
        newIntent.setAction(Long.toString(System.currentTimeMillis()));
    
        startActivity(newIntent);
        finish();
    }
    

    b)清单:

    <activity
            android:name="YOUR.NEW.FRAGMENT.ACTIVITY"
            android:label="@string/app_name"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
    
                <data android:scheme="http" />
                <data android:scheme="https" />
                <data android:scheme="scheme1" /> <!-- sheme1://-->
                <data android:host="yourdomain.com" />
                <data android:host="www.yourdomain.com" />
            </intent-filter>
        </activity>
    

    c)通过调用以下示例函数来处理活动onCreate()和onResume()中传递的新意图:

    private void handleUrl(Intent i){
        String intentUrl = null;
        if (i != null) {
            intentUrl = i.getStringExtra("intentUrl");
            if (intentUrl == null){
                //hmm intent is damaged somehow
            } else {
                //because of onResume()
                if ( i.getBooleanExtra("used",false) ) {
                    return;
                }
                i.putExtra("used", true);
    
               //DO SOMETHING WITH YOUR URL HERE
        }       
    }
    
  • 55

    只为一个实例解决此问题

    android:launchMode =“singleInstance”

    <activity
        android:name=".SplashScreen"
        android:screenOrientation="portrait"
        android:launchMode="singleInstance"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter android:autoVerify="true">
            <action android:name="android.intent.action.VIEW" />
    
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
    
            <data android:scheme="nvd.abc" />
        </intent-filter>
    </activity>
    
  • 3

    我通过在清单文件中添加android:launchMode =“singleTask”来解决这些问题

相关问题