詳解App相互喚醒的幾種方式
下文皆使用Client表示操作的App,Server表示需要被喚起的遠(yuǎn)端App,Server的包名為“com.jxx.server”
1. ComponentName
使用ComponentName喚起Server步驟很簡(jiǎn)單,需要注意的是Server的Activity需要在manifest配置種設(shè)置exported為true
Server的配置如下:
<activity android:name="com.jxx.server.ServerActivity" android:exported="true"/>
Client調(diào)用如下:
Intent intent1 = new Intent();
ComponentName componentName = new ComponentName("com.jxx.server", "com.jxx.server.ServerActivity");
intent1.setComponent(componentName);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
Intent中添加ComponentName還有另外一種寫法
Intent intent2 = new Intent();
intent2.setClassName("jxx.com.server", "jxx.com.server.MainActivity");
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
只不過(guò)setClassName內(nèi)部幫我們new ComponentName的實(shí)例
public @NonNull Intent setClassName(@NonNull String packageName, @NonNull String className) {
mComponent = new ComponentName(packageName, className);
return this;
}
既然是用Intent來(lái)喚起Activity,那就能使用Intent的特性,例如使用Bundle傳遞數(shù)據(jù)
Intent intent1 = new Intent();
ComponentName componentName = new ComponentName("com.jxx.server", "com.jxx.server.ServerActivity");
intent1.setComponent(componentName);
Bundle bundle1 = new Bundle();
bundle1.putString("remote_invoke", "from_client");
intent1.putExtras(bundle1);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
在Server端提取數(shù)據(jù)也很簡(jiǎn)單
Bundle bundle = getIntent().getExtras();
2. 隱式跳轉(zhuǎn),Uri
Android中喚起撥號(hào)頁(yè)面是這樣的
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phoneNumber));
startActivity(intent);
其實(shí)就是用Uri的形式喚起Server,并傳遞數(shù)據(jù),我們來(lái)自己實(shí)現(xiàn)一下。 這種方式下,Server端的配置如下,必須添加要有action、data以及category:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.jxx.server.ServerActivity" />
<data
android:host="com.jxx.server"
android:scheme="ServerActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter> </activity>
Client調(diào)用:
Intent intent2 = new Intent("com.jxx.server.ServerActivity");
Uri uri = Uri.parse("ServerActivity://com.jxx.server?remote_invoke=from_client");
intent2.setData(uri);
startActivity(intent2);
我們看到uri中?后面還加了"remote_invoke=from_client",這其實(shí)是用來(lái)給Server傳遞數(shù)據(jù)用的,我們可以在Server中解析出來(lái)
Uri uri = getIntent().getData();
String from = uri.getQueryParameter("remote_invoke");
//from = "from_client"
這里還有一個(gè)需要注意的點(diǎn)是,如果Client在調(diào)用時(shí)沒(méi)有指定Action,同時(shí)Server中又有多個(gè)Activity注冊(cè)了相同的scheme和host,那么在頁(yè)面跳轉(zhuǎn)時(shí),系統(tǒng)會(huì)彈框讓我們選擇跳轉(zhuǎn)到哪個(gè)頁(yè)面,如下圖所示:
3. 通過(guò)PackageManager喚起
只需要知道Server的包名即可
PackageManager packageManager = getPackageManager();
Intent intent3 = packageManager.getLaunchIntentForPackage("com.jxx.server");
if (intent3 != null) {
startActivity(intent3);
}
4. 靜態(tài)廣播接收者
只需要Server端注冊(cè)一個(gè)靜態(tài)廣播接收者,在廣播接收者中跳轉(zhuǎn)Activity即可,客戶端只需要發(fā)送一個(gè)廣播。
Server定義廣播接收者:
public class ServerBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, MainActivity.class);
//注意,這里必須要添加這個(gè)flag,
//原因在于這里的context并不是一個(gè)Activity類型的context,無(wú)法直接開(kāi)啟activity
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
并在manifest中注冊(cè)為靜態(tài)廣播接收者,并定義action
<receiver android:name=".ServerBroadCastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="server.ServerBroadCastReceiver" /> </intent-filter> </receiver>
Client中發(fā)送廣播即可
Intent intent4 = new Intent("server.ServerBroadCastReceiver");
//這里加上componentName用于解決8.0以上不能喚起的問(wèn)題
ComponentName componentName = new ComponentName("com.jxx.server", "com.jxx.server.ServerBroadCastReceiver");
intent4.setComponent(componentName);
sendBroadcast(intent4);
5. Service
在Android Service詳解(二) 中我們介紹了如何通過(guò)Service實(shí)現(xiàn)IPC通信,這當(dāng)然也能用來(lái)喚起App,這里就不再過(guò)多介紹了,有興趣的同學(xué)可以點(diǎn)擊查看。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)環(huán)信修改頭像和昵稱
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)環(huán)信修改頭像和昵稱,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
淺析Kotlin使用infix函數(shù)構(gòu)建可讀語(yǔ)法流程講解
這篇文章主要介紹了淺析Kotlin使用infix函數(shù)構(gòu)建可讀語(yǔ)法,我們?cè)贙otlin中就多次使用A to B這樣的語(yǔ)法結(jié)構(gòu)構(gòu)建鍵值對(duì),包括Kotlin自帶的mapOf()函數(shù),這種語(yǔ)法結(jié)構(gòu)的優(yōu)點(diǎn)是可讀性強(qiáng)2023-01-01
Android?Springboot?實(shí)現(xiàn)SSE通信案例詳解
SSE是一種用于實(shí)現(xiàn)服務(wù)器主動(dòng)向客戶端推送數(shù)據(jù)的技術(shù),它基于?HTTP?協(xié)議,利用了其長(zhǎng)連接特性,在客戶端與服務(wù)器之間建立一條持久化連接,并通過(guò)這條連接實(shí)現(xiàn)服務(wù)器向客戶端的實(shí)時(shí)數(shù)據(jù)推送,這篇文章主要介紹了Android?Springboot?實(shí)現(xiàn)SSE通信案例,需要的朋友可以參考下2024-07-07
Android開(kāi)發(fā)獲取手機(jī)內(nèi)網(wǎng)IP地址與外網(wǎng)IP地址的詳細(xì)方法與源碼實(shí)例
這篇文章主要介紹了Android獲取本機(jī)內(nèi)網(wǎng)IP地址與外網(wǎng)IP地址源碼實(shí)例,需要的朋友可以參考下2020-03-03
android 開(kāi)發(fā)教程之日歷項(xiàng)目實(shí)踐(一)
決定開(kāi)始學(xué)習(xí) Android 平臺(tái)下的軟件開(kāi)發(fā),以日歷作為實(shí)踐項(xiàng)目,進(jìn)行一周后,基本完成,有需要的朋友可以參考下2013-01-01
Android中RecyclerView實(shí)現(xiàn)分頁(yè)滾動(dòng)的方法詳解
RecyclerView實(shí)現(xiàn)滾動(dòng)相信對(duì)大家來(lái)說(shuō)都不陌生,但是本文主要給大家介紹了利用Android中RecyclerView實(shí)現(xiàn)分頁(yè)滾動(dòng)的思路和方法,可以實(shí)現(xiàn)翻頁(yè)功能,一次翻一頁(yè),也可以實(shí)現(xiàn)翻至某一頁(yè)功能。文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-04-04
Android BottomSheet效果的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Android BottomSheet效果的兩種實(shí)現(xiàn)方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android Init進(jìn)程對(duì)信號(hào)的處理流程詳細(xì)介紹
這篇文章主要介紹了Android Init進(jìn)程對(duì)信號(hào)的處理流程詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02

