App相互喚醒的幾種方式 [復制鏈接]

2019-3-26 10:17
一碼到底 閱讀:179 評論:0 贊:0
Tag:  

下文皆使用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);                         


我來說兩句
您需要登錄后才可以評論 登錄 | 立即注冊
facelist
所有評論(0)
領先的中文移動開發者社區
18620764416
7*24全天服務
意見反饋:[email protected]

掃一掃關注我們

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粵ICP備15117877號 )

两码中特期期