我成功地通过 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的正确方式?