使用意图(Intents)从另一个Android应用程序打开Android应用程序

huangapple go评论59阅读模式
英文:

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 = &quot;com.myapp.demo.application&quot;
        val activityName = &quot;$packageName.WelcomeActivity&quot;

        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,
                &quot;App not installed&quot;,
                Toast.LENGTH_LONG
            ).show()
        }
    }

And the manifest in the second application looks like this:

&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    package=&quot;com.myapp.demo.application&quot;&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:hardwareAccelerated=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme.NoActionBar&quot;
        android:usesCleartextTraffic=&quot;true&quot;
        tools:ignore=&quot;UnusedAttribute&quot;&gt;

        &lt;activity android:name=&quot;.SimpleScannerActivity&quot; /&gt;
        &lt;activity android:name=&quot;.MainActivity&quot; /&gt;

        &lt;activity
            android:name=&quot;.WelcomeActivity&quot;
            android:exported=&quot;true&quot;
            android:label=&quot;@string/title_activity_welcome&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

    &lt;!-- Permissions and features declarations go here --&gt;
    &lt;!-- ... --&gt;

    &lt;uses-sdk tools:overrideLibrary=&quot;com.google.zxing.client.android&quot; /&gt;

&lt;/manifest&gt;

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(&quot;com.myapp.demo&quot;);
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

 &lt;intent-filter android:label=&quot;@string/filter_view_example_gizmos&quot;&gt;
        &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
        &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
        &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
        &lt;!-- Accepts URIs that begin with &quot;demo://myapp” --&gt;
        &lt;data android:scheme=&quot;demo&quot;
              android:host=&quot;myapp&quot; /&gt;
    &lt;/intent-filter&gt;

Adding the queries tag in the manifest of appB

  &lt;queries&gt;
        &lt;package android:name=&quot;com.myapp.demo&quot;/&gt;
        &lt;intent&gt;
            &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
        &lt;/intent&gt;
    &lt;/queries&gt;

Launching the activity of appA from appB

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


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

huangapple
  • 本文由 发表于 2023年7月13日 20:11:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定