使用 Intents 从另一个 Android 应用程序打开一个 Android 应用程序

问题描述 投票:0回答:2

我在使用 Android 中的 Intents 打开我的其他应用程序之一时遇到问题。 这是我现在拥有的:

        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>

    <!-- Permissions and features declarations go here -->
    <!-- ... -->

    <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
。也许我的演示应用程序清单有问题?

android kotlin android-intent manifest
2个回答
5
投票

如果您想从 appB 启动 appA 的启动(带有类别 LAUNCHER 的活动)活动,则可以使用以下命令来完成:

val launchIntent = getPackageManager().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" />
        <!-- Accepts URIs that begin with "demo://myapp” -->
        <data android:scheme="demo"
              android:host="myapp" />
    </intent-filter>

在 appB 的清单中添加查询标签

  <queries>
        <package android:name="com.myapp.demo"/>
        <intent>
            <action android:name="android.intent.action.VIEW" />
        </intent>
    </queries>

从appB启动appA的activity

          val uri = Uri.parse("demo://myapp")
            val intent = Intent(Intent.ACTION_VIEW, uri)


            intent.resolveActivity(packageManager)?.also {
                startActivity(intent)
            }

0
投票

此代码有效,并且如果未安装应用程序或目标类不存在,则始终会抛出 ActivityNotFoundException

try {
    val intent = Intent().apply {
        component = ComponentName("com.other.app.package.name", "com.other.app.package.name.target.activity")
    }
    startActivity(intent)
} catch (e: ActivityNotFoundException) {
    Toast.makeText(
        context,
        "App is not installed! \n${e.localizedMessage}",
        Toast.LENGTH_SHORT
    ).show()
}
© www.soinside.com 2019 - 2024. All rights reserved.