如何检查Whatsapp是否安装在Android设备中?

时间:2016-04-05 07:08:19

标签: android

我想检查whatsapp是否安装在手机中,如果已安装然后显示toast“已安装”,如果没有安装,则显示Toast“Not Installed”。我该如何帮助。

5 个答案:

答案 0 :(得分:14)

您可以使用此代码。它将检查是否已安装包。

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;
    }
}

答案 1 :(得分:1)

这是获取设备中已安装应用程序的所有软件包名称的代码

 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);

获取软件包列表后,搜索com.whatsapp(官方网站Whatsapp上给出的应用程序包的名称)。多数民众赞成......

答案 2 :(得分:0)

试试这个。

您需要将包名称传递为uri。

public static boolean searchNode(Graph graph, Node start, Node end){

     Queue<Node> list = new LinkedList<Node>();
}

检查此类情况。

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;
    }

答案 3 :(得分:0)

试试这样:

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;
    }
}

答案 4 :(得分:0)

试试这个方法:

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");
}