@Subscribe(threadMode = ThreadMode.MainThread)
public void onEventMainThread(final StartShareViaWhatsappEvent event) {
String packageName = ShareModel.getPackageName("whatsapp");
if (TextUtils.isEmpty(packageName)) return;
try {
// check if installed already
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, ShareModel.SHARE_URL);
// See if official Facebook app is found
List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo info : matches)
if (info.activityInfo.packageName.toLowerCase().startsWith(packageName)) {
intent.setPackage(info.activityInfo.packageName);
this.startActivityForResult(intent, REQUEST_CODE_SHARE_VIA_WHATSAPP);
AnalyticsUtil.logEvent(AnalyticsConstDefine.buy_page_share_ok);
}
} catch (Exception e) {
// app not found
Uri marketDirectUri = Uri.parse("market://details?id=" + packageName);
Uri httpsUri = Uri.parse("https://play.google.com/store/apps/details?id=" + packageName);
//use a try/catch block here because an Exception will be thrown if the Play Store is not installed on the target device.
try {
startActivityForResult(new Intent(Intent.ACTION_VIEW, marketDirectUri), REQUEST_CODE_SHARE_VIA_WHATSAPP);
} catch (android.content.ActivityNotFoundException anfe) {
startActivityForResult(new Intent(Intent.ACTION_VIEW, httpsUri), REQUEST_CODE_SHARE_VIA_WHATSAPP);
}
}
}
【Android】Share via whatsapp
最新推荐文章于 2025-03-15 15:28:21 发布