android intent隱式調用之一個應用程式啟動另一個應用程式

來源:互聯網
上載者:User

理解Intent的關鍵之一是理解清楚Intent的兩種基本用法:一種是顯式的Intent,即在構造Intent對象時就指定接收者,這種方式與普通的函數調用類似;另一種是隱式的Intent,即Intent的寄件者在構造Intent對象時,並不知道也不關心接收者是誰,這種方式與函數調用差別比較大,有利於降低寄件者和接收者之間的耦合。另外Intent除了發送外,還可用於廣播.

顯示調用 1.Intent intent = new Intent(); intent.setClass(A.this,B.class); //intent.setClassName("com.view","com.view.B");前者是A的包名,後者是B的代包類名

startActivity(Intent);

隱式調用:private static String MY_ACTION = "com.view.my_action";

              Intent intent = new Intent();  intent.setAction(MY_ACTION);

               或者// Intent intent = new Intent(MY_ACTION);

                startActivity(Intent);

隱式調用注意不只是在AndroidManifest.xml檔案中聲明,還要加上intent-filter

在B類的activity中加上:

<intent-filter>

    <action android:name="com.view.my_action"/>  <!-- 自訂 -->

</intent-filter>

加上這句話就可以跳轉到B類了

如果是想啟動另外一個應用程式,如B類是在另外一個應用程式的入口,在其activity中再另外加上一個intent-filter

<activity android:name=".B" android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

<intent-filter>

<action android:name="com.view.my_action"/>

                                <action android:name="android.intent.action.DEFAULT" />

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</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.