如何通过我的计划隐式打开我的应用程序?

时间:2018-06-26 16:38:39

标签: java android android-intent intentfilter

我有应用“ A”需要通过隐式意图从其他应用“ B,C,D”打开

因此,在我的应用程序“ A”中,我定义了意图过滤器

<intent-filter>
    <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 "example://gizmos” -->
    <data
        android:host="applink"
        android:scheme="mobileapp"/>
</intent-filter>

在应用程序“ B”中,我定义了隐式意图

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("package.name.of.destination.app");
startActivity(LaunchIntent);

我得到这样的错误

2018-06-26 19:35:44.155 2270-2270/com.example.ezplan.deleteit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ezplan.deleteit, PID: 2270
java.lang.IllegalStateException: Could not execute method for android:onClick
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25883)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6642)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6597) 
    at android.view.View.performClickInternal(View.java:6574) 
    at android.view.View.access$3100(View.java:778) 
    at android.view.View$PerformClick.run(View.java:25883) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6642) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Intent.migrateExtraStreamToClipData()' on a null object reference
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1666)
    at android.app.Activity.startActivityForResult(Activity.java:4584)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:792)
    at android.app.Activity.startActivityForResult(Activity.java:4542)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:779)
    at android.app.Activity.startActivity(Activity.java:4903)
    at android.app.Activity.startActivity(Activity.java:4871)
    at com.example.ezplan.deleteit.MainActivity.doOnCLickButton(MainActivity.java:29)

我在做什么错了?

或者,请给我一个示例,了解如何正确完成操作

随便问

EDIT1

1)我需要打开的应用中的过滤器

<intent-filter>
            <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 "mobileapp://applink” -->
            <data
                android:host="applink"
                android:scheme="mobileapp"/>
        </intent-filter>

2)我在应用程序中尝试调用外部应用程序的代码

public void testit(View view)
{
    Intent LaunchIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mobileapp://applink"))
            .setPackage("package.name.of.destination.app");

    startActivity(LaunchIntent);
}

Exeption

  

原因:android.content.ActivityNotFoundException:找不到用于处理Intent的活动{act = android.intent.action.VIEW dat = mobileapp:// applink pkg = package.name.of.destination.app}

1 个答案:

答案 0 :(得分:1)

PackageManager.getLaunchIntentForPackage(String)的文档指出:

  

返回“好”意图在程序包中启动前门活动。例如,在浏览包时,它用于实现“打开”按钮。当前实现首先在类别QuantifierId 18 0.0 19 0.0 20 0.0 21 NaN 23 NaN 24 NaN 25 NaN 26 NaN 27 NaN 28 NaN 63 NaN 64 NaN 81 NaN 82 NaN 83 NaN 84 NaN 85 NaN 86 NaN 87 NaN 88 NaN 89 NaN 91 NaN 93 NaN 94 NaN 95 NaN 96 NaN 121 NaN Name: 52466, dtype: float64 中寻找主要活动,然后在类别Intent.CATEGORY_INFO中寻找主要活动。如果找不到,则返回Intent.CATEGORY_LAUNCHER

如果您不遵守此合同,则null为空,因此会崩溃。

通常,您在清单中定义了一个主启动程序意图过滤器:

LaunchIntent

这将由<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> 找到。在不更改或添加您已定义的意图过滤器的情况下,还可以创建适当的意图以手动匹配它:

getLaunchIntentForPackage()