I want to check that whether whatsapp is installed in mobile or not if installed then show toast "installed" and if not installed then show Toast "Not Installed".How can I do that Kindly help.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can use this code. It will check if package is installed.
public class Example extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Put the package name here... boolean installed = appInstalledOrNot("com.whatsapp"); if(installed) { System.out.println("App is already installed on your phone"); } else { System.out.println("App is not currently installed on your phone"); } } private boolean appInstalledOrNot(String uri) { PackageManager pm = getPackageManager(); boolean app_installed; try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); app_installed = true; } catch (PackageManager.NameNotFoundException e) { app_installed = false; } return app_installed; } }
回答2:
This is the code to get all package names of installed applications in device
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
after getting list of packages search for com.whatsapp(package name of whats app given on official webiste Whatsapp). Thats it..
回答3:
Try this.
Here you need to pass package name as uri.
private boolean appInstalledOrNot(String uri) { PackageManager pm = getPackageManager(); boolean app_installed; try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); app_installed = true; } catch (PackageManager.NameNotFoundException e) { app_installed = false; } return app_installed; }
Check condition like this.
if(!appInstalledOrNot("com.whatsapp")){ // Toast message not installed. }else{ // Toast message installed. }
回答4:
Try like this:
public class WhatsApp_Check extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); boolean installed = appInstalledOrNot("whatsapp_package_name"); if(installed) { //print whatsApp is already installed on your phone else{ // print whatsApp is not currently installed on your phone } } private boolean appInstalledOrNot(String uri) { PackageManager pm = getPackageManager(); boolean app_installed; try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); app_installed = true; } catch (PackageManager.NameNotFoundException e) { app_installed = false; } return app_installed; } }
回答5:
Try this method:
private void checkWhatsapp() { String packageName = "com.whatsapp"; String mesgToShare = "Hey, I am searching for Whatsapp in your device."; boolean gotPackage = false; Intent shareIntent = new Intent( android.content.Intent.ACTION_SEND ); shareIntent.setType( "text/plain" ); shareIntent.putExtra( android.content.Intent.EXTRA_TEXT, mesgToShare ); List<ResolveInfo> activityList = getPackageManager().queryIntentActivities( shareIntent, 0 ); for ( final ResolveInfo app : activityList ) { if ( (app.activityInfo.name).contains( packageName ) ) { gotPackage = true; final ActivityInfo activity = app.activityInfo; ComponentName name = new ComponentName( activity.applicationInfo.packageName, activity.name ); shareIntent.addCategory( Intent.CATEGORY_LAUNCHER ); shareIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED ); shareIntent.setComponent( name ); startActivity( shareIntent ); break; // We already found what we were looking for. Don't need to execute the rest of the Loop } } if ( !gotPackage ) Log.e("TAG", "Whatsapp is not installed in your device"); }