Android開發:顯式/隱式Intent意圖跳轉Activity總結

來源:互聯網
上載者:User

Android開發:顯式/隱式Intent意圖跳轉Activity總結
顯式跳轉

是在已知包名和類名的情況下常用的跳轉方法:

 

Intent mIntent = new Intent();mIntent.setClassName(com.android.settings,com.android.settings.Settings);mContext.startActivity(mIntent);

我們也常這麼用:

 

Intent intent = new Intent(mContext, XXActivity.class);startActivity(intent);
這是跳轉到當前應用的某個Activity,相信大家都十分熟悉,今天主要講的是如何使用隱式intent意圖跳轉 隱式跳轉1、隱式跳轉之Action跳轉假定有一個Activity在清單中是這樣的聲明的:
       

那麼我們就可以使用以下代碼進行跳轉到上面這個Activity中:
    //建立一個隱式的 Intent 對象:Action 動作    Intent intent = new Intent();    //設定 Intent 的動作為清單中指定的action    intent.setAction(customer_action_here);    startActivity(intent);

2、隱式跳轉之Category跳轉假定有一個Activity在清單中是這樣聲明的:
                                                                        

我們可以使用如下代碼進行跳轉到以上Activity:
 //建立一個隱式的 Intent 對象:Category 類別 Intent intent = new Intent(); intent.setAction(customer_action_here); //添加與清單中相同的自訂category intent.addCategory(customer_category_here); startActivity(intent);

3、隱式跳轉之Data跳轉假定有一個Activity是這樣定義的:
 < activity android:name=.DataActivity>     < intent-filter>         < category android:name=android.intent.category.DEFAULT />         < data             android:scheme=content             android:host=com.example.intentdemo             android:port=8080             android:pathPattern=.*pdf             android:mimeType=text/plain/>     < /intent-filter> < /activity>

我們可以使用如下代碼進行跳轉:
 //建立一個隱式的 Intent 對象,方法四:Date 資料 Intent intent = new Intent(); Uri uri = Uri.parse(content://com.example.intentdemo:8080/abc.pdf); //註:setData、setDataAndType、setType 這三種方法只能單獨使用,不可共用                //單獨以 setData 方法設定 URI //intent.setData(uri); //單獨以 seType 方法設定 Type //intent.setType(text/plain); //上面分步驟設定是錯誤的,要麼以 setDataAndType 方法設定 URI 及 mime type intent.setDataAndType(uri, text/plain); startActivity(intent);

清單中的daport及以下屬性時可選的,沒有必要一定添加,但是添加了port及以下屬性的話,java代碼中的Uri中要做相應的匹配。
4、隱式跳轉之調用系統應用4.1 使用瀏覽器瀏覽網頁
 //web瀏覽器 Uri uri= Uri.parse(http://www.baidu.com); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
4.2 調用地圖
 //開啟地圖查看經緯度 Uri uri = Uri.parse(geo:38.899533,-77.036476); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);

4.3 調用電話撥號(不需要撥號許可權)
    Uri uri = Uri.parse(tel:10086);    Intent intent = new Intent(Intent.ACTION_DIAL, uri);//注意區別於下面4.4的action    startActivity(intent);
4.4 調用電話直接撥號(需要撥號許可權)
    Uri uri = Uri.parse(tel:15980665805);    Intent intent = new Intent(Intent.ACTION_CALL, uri);//注意區別於上面4.3的aciton    startActivity(intent);

4.5 調用簡訊程式(無需傳送簡訊許可權,接收者自填)
    Intent intent = new Intent(Intent.ACTION_VIEW);        intent.putExtra(sms_body, 這裡寫簡訊內容);        intent.setType(vnd.android-dir/mms-sms);        startActivity(intent);
4.6 調用簡訊程式(無需傳送簡訊許可權)
    Uri uri = Uri.parse(smsto:10086);//指定接收者    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);    intent.putExtra(sms_body, 你這個黑心電訊廠商);    startActivity(intent);
4.7 調用郵件程式
    Intent intent = new Intent(Intent.ACTION_SENDTO);     intent.setData(Uri.parse(mailto:xxx@gmail.com));     intent.putExtra(Intent.EXTRA_SUBJECT, 這是標題);     intent.putExtra(Intent.EXTRA_TEXT, 這是內容);     startActivity(intent);

4.8 調用媒體播放器
    Intent intent = new Intent(Intent.ACTION_VIEW);    //Uri uri = Uri.parse(file:///sdcard/zhy.mp3);    Uri uri = Uri.parse(file:///sdcard/a.mp3);    intent.setDataAndType(uri, audio/mp3);    startActivity(intent);

4.9 調用搜尋
    Intent intent = new Intent();     intent.setAction(Intent.ACTION_WEB_SEARCH);     intent.putExtra(SearchManager.QUERY, android);     startActivity(intent);

總結相信大家經過上面的介紹,已經對Intent跳轉有了一個比較成熟的理解,Intent是組件之間的紐帶,使用它可以讓系統替我們完成很多工作,不需要我們來指定完成工作的程式。實際上,我們會發現,調用系統程式使用液無非是隱式跳轉,只不過使用的是系統內建的一些Action,Uri,Data等資訊而已。希望對大家有所協助。 




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.