我正在尝试在我的应用程序中实现深层链接,我在我的主要 Activity 的 onResume 方法中添加了 getIntent 并且我能够从链接打开我的主要 Activity ,但我面临以下问题。
如果我第一次通过单击应用程序图标打开应用程序,则 Intent 操作将为 Intent.ACTION_MAIN,这对于所有后续尝试都是不变的,即当我通过链接,intent.action 应该是 Intent.ACTION_VIEW,但操作始终是 ACTION_MAIN。
如果应用程序是通过 chrome 的链接打开的,那么我可以看到我的应用程序的两个实例,即 chrome 上方和我的应用程序本身。
<activity android:name=".MainActivity" android:hardwareAccelerated="false" android:launchMode="singleTop"> // I used singleTop because i have implementd isTaskRoot in my main activity <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <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:host="clip.myapp.tp" android:pathPattern="/.*" android:scheme="mhhds" /> </intent-filter> </activity>
下面是我实现了 getIntent 的 mainactivity.java 文件的 onResume
@Override
protected void onResume() {
super.onResume();
mIntent = getIntent();
String appLinkAction = mIntent.getAction();
if(mIntent.getAction().equals(Intent.ACTION_VIEW)) {
Uri data = mIntent.getData();
String mIntentData = data.toString();
System.out.println("Intentdata:" + mIntentData);
}
}
最佳答案
这是因为 singleTop
不会创建一个新的 Activity 实例并且总是使用现有的实例
if I open the app by clicking on the app icon for the very first time, then the intent action will be Intent.ACTION_MAIN, this will be constant for all succeeding attempts, i.e when I open the app through a link, the intent.action is supposed to be Intent.ACTION_VIEW,but the action is always ACTION_MAIN.
由于上述原因,getIntent
将返回第一次收到的实例,而不是覆盖 onNewIntent
这将返回最新 Intent 的实例,因此使用 onNewItent
而不是 onResume
if the app is opened through link from chrome, then i can see two instances of my app, i.e above chrome and my app itself
这是因为您的应用程序之前是作为独立打开的(现在在堆栈历史记录中),现在它作为搜索结果在 chrome 中打开,所以这是正常行为。
关于Android 深度链接 Intent 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52696449/