詳解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傳遞數(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ù)也很簡單
Bundle bundle = getIntent().getExtras();
2. 隱式跳轉,Uri
Android中喚起撥號頁面是這樣的
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phoneNumber)); startActivity(intent);
其實就是用Uri的形式喚起Server,并傳遞數(shù)據(jù),我們來自己實現(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調用:
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傳遞數(shù)據(jù)用的,我們可以在Server中解析出來
Uri uri = getIntent().getData(); String from = uri.getQueryParameter("remote_invoke"); //from = "from_client"
這里還有一個需要注意的點是,如果Client在調用時沒有指定Action,同時Server中又有多個Activity注冊了相同的scheme和host,那么在頁面跳轉時,系統(tǒng)會彈框讓我們選擇跳轉到哪個頁面,如下圖所示:
3. 通過PackageManager喚起
只需要知道Server的包名即可
PackageManager packageManager = getPackageManager(); Intent intent3 = packageManager.getLaunchIntentForPackage("com.jxx.server"); if (intent3 != null) { startActivity(intent3); }
4. 靜態(tài)廣播接收者
只需要Server端注冊一個靜態(tài)廣播接收者,在廣播接收者中跳轉Activity即可,客戶端只需要發(fā)送一個廣播。
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中注冊為靜態(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以上不能喚起的問題 ComponentName componentName = new ComponentName("com.jxx.server", "com.jxx.server.ServerBroadCastReceiver"); intent4.setComponent(componentName); sendBroadcast(intent4);
5. Service
在Android Service詳解(二) 中我們介紹了如何通過Service實現(xiàn)IPC通信,這當然也能用來喚起App,這里就不再過多介紹了,有興趣的同學可以點擊查看。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Kotlin 使用高階函數(shù)實現(xiàn)回調方式
這篇文章主要介紹了Kotlin 使用高階函數(shù)實現(xiàn)回調方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android數(shù)據(jù)類型之間相互轉換系統(tǒng)介紹
一些初學Android的朋友可能會遇到JAVA的數(shù)據(jù)類型之間轉換的苦惱;本文將為有這類需求的朋友解決此類問題2012-11-11Android開發(fā)Jetpack組件ViewModel與LiveData使用講解
Jetpack是一個由多個技術庫組成的套件,可幫助開發(fā)者遵循最佳做法,減少樣板代碼并編寫可在各種Android版本和設備中一致運行的代碼,讓開發(fā)者精力集中編寫重要的代碼2022-09-09當ListView有Header時 onItemClick里的position不正確的原因
這篇文章主要介紹了當ListView有Header時 onItemClick里的position不正確的原因的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07