我正试图启动打开Android市场链接的意图。
android manifest部分看起来像这样:
<activity android:name="com.surreall.sixdice.Start" android:label="Six Dice" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
启动意图如下:
public void onClick(View v) {
String PublisherID = "pub:surreallgames";
Uri marketUri = Uri.parse("market://search?q="+PublisherID);
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
startActivity(marketIntent);
}
logcat输出:
06-17 17:38:47.393: E/AndroidRuntime(476): FATAL EXCEPTION: main
06-17 17:38:47.393: E/AndroidRuntime(476): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://search?q=pub:surreallgames }
06-17 17:38:47.393: E/AndroidRuntime(476): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
06-17 17:38:47.393: E/AndroidRuntime(476): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
06-17 17:38:47.393: E/AndroidRuntime(476): at android.app.Activity.startActivityForResult(Activity.java:2827)
06-17 17:38:47.393: E/AndroidRuntime(476): at android.app.Activity.startActivity(Activity.java:2933)
06-17 17:38:47.393: E/AndroidRuntime(476): at com.surreall.sixdice.Start$9.onClick(Start.java:265)
06-17 17:38:47.393: E/AndroidRuntime(476): at android.view.View.performClick(View.java:2485)
06-17 17:38:47.393: E/AndroidRuntime(476): at android.view.View$PerformClick.run(View.java:9080)
06-17 17:38:47.393: E/AndroidRuntime(476): at android.os.Handler.handleCallback(Handler.java:587)
06-17 17:38:47.393: E/AndroidRuntime(476): at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 17:38:47.393: E/AndroidRuntime(476): at android.os.Looper.loop(Looper.java:123)
06-17 17:38:47.393: E/AndroidRuntime(476): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-17 17:38:47.393: E/AndroidRuntime(476): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 17:38:47.393: E/AndroidRuntime(476): at java.lang.reflect.Method.invoke(Method.java:507)
06-17 17:38:47.393: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-17 17:38:47.393: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-17 17:38:47.393: E/AndroidRuntime(476): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:79)
您在缺少Google Play商店的Android环境中运行此代码,例如模拟器,Kindle Fire等。
如果您在模拟器上遇到此问题,请在具有Play商店的设备上测试此代码路径。
如果您在某些缺少Play商店的硬件上遇到此问题,或者您计划将应用分发给缺少Play商店的设备,请处理例外或使用PackageManager
和{{ 1}}确定在调用resolveActivity()
之前您的Intent
是否会成功。
startActivity()
答案 1 :(得分:15)
您可以使用Intent.createChooser()方法安全地启动Intent。如果不存在可以处理您的意图的应用程序,则会显示一个对话框,告诉用户这一点。但是,如果安装了Google Play商店,则用户可以选择在Play商店中“打开”该商标。
startActivity(Intent.createChooser(marketIntent, "dialogTitle"));
答案 2 :(得分:12)
更好的解决方案是尝试在Google Play应用中打开uri,但如果没有此类应用(没有活动来处理此意图) - 您可以尝试在浏览器中打开uri,如下例所示:
public static void rateApp(Context context) {
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
} catch (android.content.ActivityNotFoundException anfe) {
viewInBrowser(context, "https://play.google.com/store/apps/details?id=" + context.getPackageName());
}
}
public static void viewInBrowser(Context context, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (null != intent.resolveActivity(context.getPackageManager())) {
context.startActivity(intent);
}
}
答案 3 :(得分:5)
尝试添加
<category android:name="android.intent.category.DEFAULT" />
到调用Activity。
答案 4 :(得分:2)
private void OpenStoreIntent(){
String url="";
Intent storeintent=null;
try {
url = "market://details?id=com.myapp.packagename";
storeintent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
storeintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
context.startActivity(storeintent);
} catch ( final Exception e ) {
url = "https://play.google.com/store/apps/details?id=com.myapp.packagename";
storeintent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
storeintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
context.startActivity(storeintent);
}
}
答案 5 :(得分:0)
我遇到了同样的错误,因为我写了:
Linking.openURL('www.somewebsite.com');
将URL更改为http://somewebsite.com
(或https://somewebsite.com
,具体取决于您定位的网站)解决了问题:)