How to launch an Activity from another Application in Android

后端 未结 12 1468
春和景丽
春和景丽 2020-11-21 06:02

I want to launch an installed package from my Android application. I assume that it is possible using intents, but I didn\'t find a way of doing it. Is there a link, where t

12条回答
  •  梦谈多话
    2020-11-21 06:24

    Here is my example of launching bar/QR code scanner from my app if someone finds it useful

    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
    intent.setPackage("com.google.zxing.client.android");
    
    try 
    {
        startActivityForResult(intent, SCAN_REQUEST_CODE);
    } 
    catch (ActivityNotFoundException e) 
    {
        //implement prompt dialog asking user to download the package
        AlertDialog.Builder downloadDialog = new AlertDialog.Builder(this);
        downloadDialog.setTitle(stringTitle);
        downloadDialog.setMessage(stringMessage);
        downloadDialog.setPositiveButton("yes",
                new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialogInterface, int i) 
                    {
                        Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        try
                        {
                            myActivity.this.startActivity(intent);
                        }
                        catch (ActivityNotFoundException e)
                        {
                            Dialogs.this.showAlert("ERROR", "Google Play Market not found!");
                        }
                    }
                });
        downloadDialog.setNegativeButton("no",
                new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int i) 
                    {
                        dialog.dismiss();
                    }
                });
        downloadDialog.show();
    }
    

提交回复
热议问题