在Android中启动第三方应用程序的安装

时间:2012-11-16 09:26:55

标签: android android-intent google-play

我希望提示我的应用用户安装zxing条形码扫描仪(如果没有)。这是我用来加载对话框并要求用户下载应用程序的代码,但它目前正在返回“找不到活动”相关错误:

清单:

<activity
    android:name=".FrontActivity"
    android:label="@string/title_activity_front"
    android:screenOrientation = "portrait" 
    android:theme="@style/NoTitleBar" >

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter> 
        <action android:name="android.intent.action.VIEW" />  
        <category android:name="android.intent.category.DEFAULT" />  
    </intent-filter> 

</activity>

的java:

public class FrontActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.front_activity);


        isScannerInstalled();
    }

    private void isScannerInstalled()
    {
        try
        {
            ApplicationInfo info = 
                getPackageManager().
                getApplicationInfo("com.google.zxing.client.android",0);
        } 

        catch (PackageManager.NameNotFoundException e)
        {
            AlertDialog.Builder alertDialogBuilder = 
                new AlertDialog.Builder(FrontActivity.this);

            alertDialogBuilder.setTitle("Warning");
            alertDialogBuilder.setMessage("Please install the Barcode Scanner.");
            alertDialogBuilder.setCancelable(false);

            alertDialogBuilder.setNeutralButton
            (
                "OK", 
                new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int whichButton) 
                    {
                          Uri uri = Uri.parse(
                          "market://search?q=pname:com.google.zxing.client.android"
                          );

                          startActivity(new Intent(Intent.ACTION_VIEW, uri));
                    }
                }
            );

            alertDialogBuilder.show();
        }
    }
}

错误:

11-16 08:37:37.716: E/AndroidRuntime(12694): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://search?q=pname:com.google.zxing.client.android }
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.app.Activity.startActivityForResult(Activity.java:2817)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.app.Activity.startActivity(Activity.java:2923)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at test.app01.FrontActivity$1.onClick(FrontActivity.java:140)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.os.Looper.loop(Looper.java:123)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.app.ActivityThread.main(ActivityThread.java:4627)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at java.lang.reflect.Method.invokeNative(Native Method)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at java.lang.reflect.Method.invoke(Method.java:521)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at dalvik.system.NativeStart.main(Native Method)

任何人都可以看到问题所在吗?

1 个答案:

答案 0 :(得分:0)

试试这个

public void onClick(DialogInterface dialog, int whichButton) 
                    {
                          try 
                          {
                               startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+packageName)));
                              }
                         catch (android.content.ActivityNotFoundException err) 
                         {
                             startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id="+packageName)));
                             }
                    }

并在设备上测试...因为模拟器没有市场应用程序..