android - 如何启动第三方应用程序的 Intent ?

标签 android android-intent

我设法启动了一个带有 Intent 的应用程序,如下所示:

public void startNewActivity(String packageName) {
    Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
    if (intent == null) {
        // Bring user to the market or let them choose an app?
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=" + packageName));
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mContext.startActivity(intent);
}

这样调用:

this.startNewActivity("com.seleuco.mame4droid");

这行得通,但我想运行一个 Intent ,不仅要启动应用程序,还要直接运行游戏。

MAME4Droid 的 list 是这样说的:

    <activity android:name="com.seleuco.mame4droid.MAME4droid" android:label="@string/app_name"
      android:configChanges="keyboardHidden|orientation|screenSize"
      android:launchMode="singleTask"
      android:windowSoftInputMode="stateAlwaysHidden" android:theme="@style/Theme.MAME4droid">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:host="" android:scheme="file" />
            <data android:mimeType="application/zip" />
        </intent-filter>
    </activity>

但我想不出调用 VIEW 操作的正确方法。

我试过这个:(注释行是我试过的变体)

public void startMAME4DroidGame(String game) {
    Toast.makeText(mContext, game, Toast.LENGTH_SHORT).show();
    String packageName = "com.seleuco.mame4droid";
    try {
            //Intent intent = new Intent(packageName + "/com.seleuco.mame4droid.MAME4droid");
            Intent intent = new Intent("com.seleuco.mame4droid/android.intent.action.VIEW");
            //intent.setAction(Intent.ACTION_VIEW);
            //intent.setData(Uri.parse("file://" + game));
            intent.setData(Uri.parse("file:" +   // also tried with "file://" +
            "/sdcard/"+Environment.DIRECTORY_DOWNLOADS+"/"+game));
            mContext.startActivity(intent);
    } catch(Exception e) {
            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

导致:

    No activity found to handle Intent
    { act=com.seleuco.mame4droid.MAME4Droid/android.intent.action.VIEW 
    dat=file:///sdcard/Download/starforc.zip }

针对特定 Activity 制定 Intent 的正确方法是什么?

最佳答案

带有操作 View 的 Intent 和文件的 URI 作为数据就可以了。我刚试过:

adb shell am start -a android.intent.action.VIEW -d file:///storage/emulated/0/MAME4droid/roms/myrom.zip -n com.seleuco.mame4droid/com.seleuco.mame4droid.MAME4droid

storage/emulated/0/MAME4droid/roms/myrom.zip 是我的 rom 文件。它会打开应用程序并开始游戏。

它也适用于应用程序:

final Intent intent = new Intent(Intent.ACTION_VIEW)
        .setData(Uri.parse("content:///storage/emulated/0/MAME4droid/roms/myrom.zip"))
        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        .setClassName("com.seleuco.mame4droid", "com.seleuco.mame4droid.MAME4droid");
context.startActivity(intent);

但是在 Android 24+ 上我必须在 list 中声明一个文件提供者:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

以及带有路径的 xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>

不知道为什么,但是当我从应用程序启动 Intent 时,只有 content 方案有效,file 无效。

关于android - 如何启动第三方应用程序的 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53711520/

相关文章:

android - 如何通过 Espresso 检查拨号器上的电话号码 - 通过 autoLink 触发的拨号器

android - 从 BroadcastReceiver 启动 Activity

android - 如何使用 RxAndroid 和 Retrofit 2 检索响应主体?

Android AsyncTask 卡住 UI

android - AlarmManager 以不一致的方式发送带有旧数据的未决 Intent

android - 在 Android 中启动服务调用应用程序 onCreate

android - 在 Android 应用程序中使用 startActivityForResult

android - 在 layout.main 上崩溃

android - 启动 Crashlytics/Fabric Beta 应用程序的 Intent 是什么?

android - 有没有办法避免在带有自定义 View (Android)的 xml 中使用完整的包名称?