日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

App相互唤醒的几种方式

發布時間:2024/1/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 App相互唤醒的几种方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下文皆使用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 {@Overridepublic void onReceive(Context context, Intent intent) {Intent intent1 = new Intent(context, MainActivity.class);//注意,這里必須要添加這個flag,//原因在于這里的context并不是一個Activity類型的context,無法直接開啟activityintent1.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,這里就不再過多介紹了,有興趣的同學可以點擊查看。

轉載于:https://juejin.im/post/5c91fde2f265da60ea145b80

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的App相互唤醒的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。