1. 程式人生 > >安卓通過url開啟app,以及下載對應app

安卓通過url開啟app,以及下載對應app

一.使用Custom Scheme URI開啟APP

    就Android平臺而言,URI主要分三個部分:scheme, authority and path。其中authority又分為host和port。格式如下:

scheme://host:port/path
舉個實際的例子:
content://com.example.project:200/folder/subfolder/etc ?arg0=1&arg1=2
———/ —————————/ —/ ————————–/ ————————–/(query我們要傳的值)
scheme host port path (我們要傳的值如果是傳多個值使用&)
——————————–/
authority

  1. android程式碼而言:主要有兩塊一部分是註冊檔案如下。
            <intent-filter>
                <data
                    android:host="com.example.project"
                    android:port="200"
                    android:scheme="content" />
                <action android:name="android.intent.action.VIEW"
/>
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter>

2.另一部分就是app首頁解析網頁程式碼如下。

 Intent intent = getIntent();  
        Uri uri=intent.getData();  
        if
(uri!=null){ String name=uri.getQueryParameter("arg0"); String name1=uri.getQueryParameter("arg1"); String scheme= uri.getScheme(); String host=uri.getHost(); String port=uri.getPort()+""; String path=uri.getPath(); String query=uri.getQuery(); }

二.通過包名開啟第三方app,如果沒有包名就去指定地址下載對應的app

    try {
            Intent intent = wm.getPackageManager().getLaunchIntentForPackage(appPackageName);
            Bundle bundle = new Bundle();
            bundle.putString("s","d");
            intent.putExtras(bundle);
            wm.startActivity(intent);
            return;
        } catch (Exception e) {
            downloadAddress = downloadAdd;
            BaseDialog dialog = new BaseDialog();
            dialog.setTitle("提示");
            dialog.setContent("您還未下載“" + DelegateDataBase.ENTRUST_LIST[index] + "”是否進行下載?");
            dialog.setConfirm("下載", new BaseDialog.DialogListener() {
                @Override
                public void onListener() {
                        Uri uri = Uri.parse(downloadAddress.toString());
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        wm.startActivity(intent);
                    }

            });
            dialog.setCancel("取消", null);
            dialog.show(wm);
        }