您好,登錄后才能下訂單哦!
下文皆使用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還有另外一種寫(xiě)法
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即可,客戶(hù)端只需要發(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類(lèi)型的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í)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。