Viber 分享按钮在 Android 中抛出异常
Viber share button throws exception in Android
我正在开发 Android 应用程序。在我的应用程序中,我添加了 Viber 共享功能。于是上网搜索"How to share a content with viber in android"。然后我发现了 Viber 的业务。所以对于分享按钮,我遵循了这个link、https://www.viber.com/en/viber-share-button。如您所见,它非常简单。它只是要求重定向到带有 url 的浏览器。所以我用 url 打开浏览器在 viber 上分享如下。
private void shareOnViberClick()
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("viber://forward?text=textIWantToShare"));
startActivity(browserIntent);
}
但是当我点击分享按钮并调用该方法时,它会抛出以下错误
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: FATAL EXCEPTION: main
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=viber://forward?text=မန္ယူအသင္း၏ ပရိတ္သတ္ဟုထုတ္ေဖာ္ေျပာျပခဲ့သည့္ ေဒါ့မြန္လူငယ္ၾကယ္ပြင့္ }
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3390)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3351)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:3587)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:3555)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at tonightfootballreport.com.tfr.NewsDetailsActivity.shareOnViberClick(NewsDetailsActivity.java:364)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at tonightfootballreport.com.tfr.NewsDetailsActivity.access0(NewsDetailsActivity.java:47)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at tonightfootballreport.com.tfr.NewsDetailsActivity.onClick(NewsDetailsActivity.java:134)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.view.View.performClick(View.java:4240)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17721)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:730)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
09-07 05:33:02.637 11715-11830/tonightfootballreport.com.tfr D/dalvikvm: GC_FOR_ALLOC freed 601K, 8% free 9230K/10032K, paused 56ms, total 60ms
09-07 05:33:05.029 11715-11833/tonightfootballreport.com.tfr D/dalvikvm: GC_FOR_ALLOC freed 258K, 6% free 10866K/11548K, paused 8ms, total 8ms
为什么会抛出该错误?我关注的 Viber 分享 API 是否正确?
不能保证 activity 和 intent-filter
符合您的要求,因此 startActivity()
可能会失败,这应该在您的代码中处理。如果发生这种情况,它会抛出 ActivityNotFoundException
,因此只需将 startActivity()
换成 try/catch
这意味着设备没有任何意图启动当前指定的意图。所以,请检查条件 (smsIntent.resolveActivity(mPackageManager) != null) 以便我们可以向用户显示任何消息
尝试使用
Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage("com.viber.voip");
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "Message body");
If you prepend http:// may be problem solved.
Uri uri = Uri.parse("http://www.google.com");
Like in your case may be work around with viber url
.
Uri.parse("http://viber:forward?text=textIWantToShare")
编辑 1:
May be they are using this url scheme only for web we not be able to use it through our app.
So may be you can do this way.
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = context.getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase(
Locale.getDefault()).contains("com.viber.voip")
|| info.activityInfo.name.toLowerCase(
Locale.getDefault()).contains("com.viber.voip")) {
share.putExtra(Intent.EXTRA_TEXT, "Your text to share");
share.setPackage(info.activityInfo.packageName);
found = true;
context.startActivity(Intent.createChooser(share, "Select"));
break;
}
}
if (!found) {
displayToast(context, "Install viber android application");
Uri marketUri = Uri.parse("market://details?id="
+ "com.viber.voip");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
context.startActivity(marketIntent);
}
}
And also whatever Marcin sir have suggested in his answer
.
我正在开发 Android 应用程序。在我的应用程序中,我添加了 Viber 共享功能。于是上网搜索"How to share a content with viber in android"。然后我发现了 Viber 的业务。所以对于分享按钮,我遵循了这个link、https://www.viber.com/en/viber-share-button。如您所见,它非常简单。它只是要求重定向到带有 url 的浏览器。所以我用 url 打开浏览器在 viber 上分享如下。
private void shareOnViberClick()
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("viber://forward?text=textIWantToShare"));
startActivity(browserIntent);
}
但是当我点击分享按钮并调用该方法时,它会抛出以下错误
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: FATAL EXCEPTION: main
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=viber://forward?text=မန္ယူအသင္း၏ ပရိတ္သတ္ဟုထုတ္ေဖာ္ေျပာျပခဲ့သည့္ ေဒါ့မြန္လူငယ္ၾကယ္ပြင့္ }
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3390)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3351)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:3587)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:3555)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at tonightfootballreport.com.tfr.NewsDetailsActivity.shareOnViberClick(NewsDetailsActivity.java:364)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at tonightfootballreport.com.tfr.NewsDetailsActivity.access0(NewsDetailsActivity.java:47)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at tonightfootballreport.com.tfr.NewsDetailsActivity.onClick(NewsDetailsActivity.java:134)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.view.View.performClick(View.java:4240)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17721)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:730)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
09-07 05:33:02.637 11715-11830/tonightfootballreport.com.tfr D/dalvikvm: GC_FOR_ALLOC freed 601K, 8% free 9230K/10032K, paused 56ms, total 60ms
09-07 05:33:05.029 11715-11833/tonightfootballreport.com.tfr D/dalvikvm: GC_FOR_ALLOC freed 258K, 6% free 10866K/11548K, paused 8ms, total 8ms
为什么会抛出该错误?我关注的 Viber 分享 API 是否正确?
不能保证 activity 和 intent-filter
符合您的要求,因此 startActivity()
可能会失败,这应该在您的代码中处理。如果发生这种情况,它会抛出 ActivityNotFoundException
,因此只需将 startActivity()
换成 try/catch
这意味着设备没有任何意图启动当前指定的意图。所以,请检查条件 (smsIntent.resolveActivity(mPackageManager) != null) 以便我们可以向用户显示任何消息
尝试使用
Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage("com.viber.voip");
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "Message body");
If you prepend http:// may be problem solved.
Uri uri = Uri.parse("http://www.google.com");
Like in your case may be work around with viber url
.
Uri.parse("http://viber:forward?text=textIWantToShare")
编辑 1:
May be they are using this url scheme only for web we not be able to use it through our app.
So may be you can do this way.
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = context.getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase(
Locale.getDefault()).contains("com.viber.voip")
|| info.activityInfo.name.toLowerCase(
Locale.getDefault()).contains("com.viber.voip")) {
share.putExtra(Intent.EXTRA_TEXT, "Your text to share");
share.setPackage(info.activityInfo.packageName);
found = true;
context.startActivity(Intent.createChooser(share, "Select"));
break;
}
}
if (!found) {
displayToast(context, "Install viber android application");
Uri marketUri = Uri.parse("market://details?id="
+ "com.viber.voip");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
context.startActivity(marketIntent);
}
}
And also whatever Marcin sir have suggested in his answer
.