英文:
Opening an Android App from another Android App using Intents
问题
我在Android中使用Intents打开我的另一个应用程序时遇到了问题。
这是我现在拥有的内容:
private fun openWeScanUI5(context: Context, list: List<String>) {
val packageName = "com.myapp.demo.application"
val activityName = "$packageName.WelcomeActivity"
try {
val intent = Intent().apply {
setClassName(packageName, activityName)
action = Intent.ACTION_MAIN
addCategory(Intent.CATEGORY_LAUNCHER)
}
startActivity(intent)
requireActivity().finish()
} catch (e: Exception) {
Toast.makeText(
context,
"App not installed",
Toast.LENGTH_LONG
).show()
}
}
而第二个应用程序的清单如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.myapp.demo.application">
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar"
android:usesCleartextTraffic="true"
tools:ignore="UnusedAttribute">
<activity android:name=".SimpleScannerActivity" />
<activity android:name=".MainActivity" />
<activity
android:name=".WelcomeActivity"
android:exported="true"
android:label="@string/title_activity_welcome">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- 权限和功能声明在此处 -->
<!-- ... -->
<uses-sdk tools:overrideLibrary="com.google.zxing.client.android" />
</manifest>
我总是得到这个异常:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.myapp.demo.application/com.myapp.demo.application.WelcomeActivity}; have you declared this activity in your AndroidManifest.xml?
我在这里漏掉了什么?
我尝试使用不同的包名启动另一个应用程序,同样的代码可以工作,但对于我的 com.myapp.demo.application 不起作用。也许我的演示应用程序清单有问题?
英文:
I am having an issue opening one of my other application using Intents in Android.
Here is what I have right now:
val packageName = "com.myapp.demo.application"
val activityName = "$packageName.WelcomeActivity"
try {
val intent = Intent().apply {
setClassName(packageName, activityName)
action = Intent.ACTION_MAIN
addCategory(Intent.CATEGORY_LAUNCHER)
}
startActivity(intent)
requireActivity().finish()
} catch (e: Exception) {
Toast.makeText(
context,
"App not installed",
Toast.LENGTH_LONG
).show()
}
}
And the manifest in the second application looks like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.myapp.demo.application">
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar"
android:usesCleartextTraffic="true"
tools:ignore="UnusedAttribute">
<activity android:name=".SimpleScannerActivity" />
<activity android:name=".MainActivity" />
<activity
android:name=".WelcomeActivity"
android:exported="true"
android:label="@string/title_activity_welcome">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- Permissions and features declarations go here -->
<!-- ... -->
<uses-sdk tools:overrideLibrary="com.google.zxing.client.android" />
</manifest>
I am always getting this exception:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.myapp.demo.application/com.myapp.demo.application.WelcomeActivity}; have you declared this activity in your AndroidManifest.xml?
What I am missing here?
I have tried starting another application with different package name and it worked with the same code, but not for my com.myapp.demo.application. Maybe something is not right with my demo application manifest?
答案1
得分: 1
如果你想从appB启动appA的LAUNCHER类别的活动,可以使用以下方式:
val launchIntent = packageManager.getLaunchIntentForPackage("com.myapp.demo")
if (launchIntent != null) {
startActivity(launchIntent)
}
如果你想打开其他活动,请为该活动创建深度链接URI。
在appA的manifest.xml中添加data标签
<intent-filter android:label="@string/filter_view_example_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- 接受以“demo://myapp”开头的URI -->
<data android:scheme="demo"
android:host="myapp" />
</intent-filter>
在appB的清单中添加queries标签
<queries>
<package android:name="com.myapp.demo"/>
<intent>
<action android:name="android.intent.action.VIEW" />
</intent>
</queries>
从appB启动appA的活动
val uri = Uri.parse("demo://myapp")
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.resolveActivity(packageManager)?.also {
startActivity(intent)
}
英文:
If you want to launch the start(activity with the catergory LAUNCHER ) activity of your appA from appB, then it can be done using :
val launchIntent = getPackageManager().getLaunchIntentForPackage("com.myapp.demo");
if (launchIntent != null) {
startActivity(launchIntent);
And if you want to open some other activity then create a deep link uri for that activity.
Add the data tag in manifest.xml of appA
<intent-filter android:label="@string/filter_view_example_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "demo://myapp” -->
<data android:scheme="demo"
android:host="myapp" />
</intent-filter>
Adding the queries tag in the manifest of appB
<queries>
<package android:name="com.myapp.demo"/>
<intent>
<action android:name="android.intent.action.VIEW" />
</intent>
</queries>
Launching the activity of appA from appB
val uri = Uri.parse("demo://myapp")
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.resolveActivity(packageManager)?.also {
startActivity(intent)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论