android:scheme 通過uri跳轉到APP應用指定Activity

來源:互聯網
上載者:User

標籤:

最近公司有個需求,點擊第三方APP的H5頁面然後直接跳轉到APP裡面,比如瀏覽器開啟一個H5頁面,然後點擊某個連結就直接到APP的某個指定的Activity,經過研究發現實現還是蠻easy的,下面說下方法吧。

首先,在AndroidManifest.xml裡面進行配置,在對應的Activity加上一個intent-filter, 如下:

<application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/title_activity_main" >            <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"/>                <category android:name="android.intent.category.BROWSABLE"/>                <data android:scheme="paraches" />            </intent-filter>            </activity>    </application>

接下來在對應的這個Activity加上相關解析代碼,如下:

@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Intent intent = getIntent();        String action = intent.getAction();        if (Intent.ACTION_VIEW.equals(action)) {        Uri uri = intent.getData();        if (uri != null) {                String host = uri.getHost();                String dataString = intent.getDataString();                String id = uri.getQueryParameter("id");                String path = uri.getPath();                String path1 = uri.getEncodedPath();                String queryString = uri.getQuery();                Log.d("Alex", "host:"+host);                Log.d("Alex", "dataString:" + dataString);                Log.d("Alex", "id:" + id);                Log.d("Alex", "path:" + path);                Log.d("Alex", "path1:" + path1);                Log.d("Alex", "queryString:" + queryString);            }        }    }

看下Log情況,如下:

host:schemedemodataString:paraches://schemedemo/get/info?id=10000id:10000path:/get/infopath1:/get/infoqueryString:id=10000

簡單網頁如下,這個網頁可以用瀏覽器開啟,然後點擊跳轉連結:

<a href="paraches://schemedemo/get/info?id=10000">open android app</a>

經過以上步驟就OK了,是不是很easy啊,不過我相信可能還是有小夥伴會迷路,這裡上傳一下項目demo,這個網頁就自己建立了,我就不上傳了,連結如下:

http://download.csdn.net/detail/msn465780/9516335

點擊開啟連結


android:scheme 通過uri跳轉到APP應用指定Activity

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.