如何启动第三方应用程序的意图?

3

我成功地通过 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");

这个方法可以实现,但我想要通过一个意图来启动游戏而不仅仅是启动应用程序。

MAME4Droid的清单文件如下:

    <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 }

什么是针对该特定活动创建Intent的正确方式?

设备上安装了meme4droid应用程序吗? - lelloman
是的,MAME4Droid已经正确安装。 - Sebastián Grignoli
2个回答

2

使用动作为VIEW和数据为文件URI的Intent即可完成操作。我刚刚尝试了以下内容:

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+上,我必须在清单文件中声明一个文件提供程序:

<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>

同时,下面是路径为xxx的xml文件:

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

我不确定为什么,但当我从应用程序启动Intent时,只有content方案可用,file无法使用。


哇,关于Android我还有很多不懂的地方...谢谢!我会尝试的。 - Sebastián Grignoli
耶!玩得开心:D - lelloman

1
如果您拥有这两个应用程序,您可以在第一个应用程序中添加一个url模式(深度链接),并使用它来处理所有内容。它使您可以使用简单的url打开该活动,例如mame4droid://openMyAct / file-uri。您必须在第一个应用程序的AndroidManifest中注册此模式,并处理这些意图以执行您想要的操作。
另一种需要使用相同签名签署的两个apk的方法是您正在尝试的方法,您确定这两个apk都使用相同的签名进行签署吗?
另外,如果您只想打开那种文件,您必须注册扩展名(如您所做的那样),并执行类似以下操作:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), fileMimeType);
startActivity(intent);

我并不拥有Seleuco的MAME4Droid。我只是想使用他在该应用程序的AndroidManifest中注册的意图。 - Sebastián Grignoli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接