国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

詳解App相互喚醒的幾種方式

2019-12-12 00:16:17
字體:
來源:轉載
供稿:網友

下文皆使用Client表示操作的App,Server表示需要被喚起的遠端App,Server的包名為“com.jxx.server”

1. ComponentName

使用ComponentName喚起Server步驟很簡單,需要注意的是Server的Activity需要在manifest配置種設置exported為true

Server的配置如下:

<activity android:name="com.jxx.server.ServerActivity" android:exported="true"/>   

Client調用如下:

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); 

只不過setClassName內部幫我們new ComponentName的實例

public @NonNull Intent setClassName(@NonNull String packageName, @NonNull String className) { mComponent = new ComponentName(packageName, className); return this;}

既然是用Intent來喚起Activity,那就能使用Intent的特性,例如使用Bundle傳遞數據

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端提取數據也很簡單

Bundle bundle = getIntent().getExtras();

2. 隱式跳轉,Uri

Android中喚起撥號頁面是這樣的

Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phoneNumber));startActivity(intent);

其實就是用Uri的形式喚起Server,并傳遞數據,我們來自己實現一下。 這種方式下,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調用:

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",這其實是用來給Server傳遞數據用的,我們可以在Server中解析出來

Uri uri = getIntent().getData();String from = uri.getQueryParameter("remote_invoke");//from = "from_client"

這里還有一個需要注意的點是,如果Client在調用時沒有指定Action,同時Server中又有多個Activity注冊了相同的scheme和host,那么在頁面跳轉時,系統會彈框讓我們選擇跳轉到哪個頁面,如下圖所示:

 

3. 通過PackageManager喚起

只需要知道Server的包名即可

PackageManager packageManager = getPackageManager();       Intent intent3 = packageManager.getLaunchIntentForPackage("com.jxx.server");if (intent3 != null) {               startActivity(intent3);             } 

4. 靜態廣播接收者

只需要Server端注冊一個靜態廣播接收者,在廣播接收者中跳轉Activity即可,客戶端只需要發送一個廣播。

Server定義廣播接收者:

public class ServerBroadCastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {  Intent intent1 = new Intent(context, MainActivity.class);  //注意,這里必須要添加這個flag,  //原因在于這里的context并不是一個Activity類型的context,無法直接開啟activity  intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  context.startActivity(intent1); }}

并在manifest中注冊為靜態廣播接收者,并定義action

<receiver               android:name=".ServerBroadCastReceiver"       android:enabled="true"           android:exported="true">          <intent-filter>              <action android:name="server.ServerBroadCastReceiver" /> </intent-filter>                        </receiver>

Client中發送廣播即可

Intent intent4 = new Intent("server.ServerBroadCastReceiver"); //這里加上componentName用于解決8.0以上不能喚起的問題          ComponentName componentName = new ComponentName("com.jxx.server", "com.jxx.server.ServerBroadCastReceiver");intent4.setComponent(componentName);                  sendBroadcast(intent4); 

5. Service

Android Service詳解(二) 中我們介紹了如何通過Service實現IPC通信,這當然也能用來喚起App,這里就不再過多介紹了,有興趣的同學可以點擊查看。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高密市| 龙州县| 灵丘县| 凤城市| 雅江县| 长武县| 温宿县| 宝鸡市| 清徐县| 黑河市| 慈利县| 织金县| 项城市| 马龙县| 崇信县| 德格县| 寿宁县| 舒城县| 辽中县| 久治县| 民权县| 长治县| 新乡县| 佛教| 嘉善县| 南召县| 昌平区| 武陟县| 明溪县| 延庆县| 东丰县| 三门县| 卫辉市| 东城区| 若羌县| 崇明县| 平南县| 襄垣县| 贺兰县| 开鲁县| 汶上县|