The following uses Client as the operating App and Server as the remote App to be invoked. The package name of Server is “com.jxx.server”.

1. ComponentName

Invoking the Server with ComponentName is a simple step. Note that the activities of the Server need to be set to true exported in the manifest configuration

The Server configuration is as follows:

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

The Client call is as follows:

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);                                                                          
Copy the code

There’s another way to add ComponentName to an Intent

Intent intent2 = new Intent();                                                                   
intent2.setClassName("jxx.com.server"."jxx.com.server.MainActivity");                                                             
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                                                 
startActivity(intent1); 
Copy the code

It’s just that setClassName internally helps us with an instance of New ComponentName

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

Since you are evoking an Activity with an Intent, you can use features of an Intent, such as using a Bundle to pass data

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);
Copy the code

Extracting data on the Server side is also simple

Bundle bundle = getIntent().getExtras();
Copy the code

2. Implicit jump, Uri

In Android, the call up page looks like this

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

Call up the Server in the form of a Uri and pass the data. Let’s do it ourselves. In this mode, the Server configuration is as follows. Action, data, and category must be added:

<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>                                                            
Copy the code

The Client call:

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);                                                          
Copy the code

We see in the URI? Remote_invoke =from_client = remote_invoke=from_client = remote_invoke=from_client

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

Another point to note here is that if the Client does not specify an Action when calling, and there are multiple activities registered with the same scheme and host in the Server, the system will pop up a box asking us to choose which page to jump to, as shown in the following figure:

3. Log in to the PackageManager

You only need to know the package name of the Server

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

4. Static broadcast receiver

The Server only needs to register a static broadcast receiver, jump the Activity in the broadcast receiver, and the client only needs to send a broadcast.

The Server defines the broadcast receiver:

public class ServerBroadCastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent intent1 = new Intent(context, MainActivity.class); // Note that this flag must be added here, because the context is not an Activity context, Intent1. setFlags(intent.flag_activity_new_task) cannot be started directly; context.startActivity(intent1); }}Copy the code

Register as a static broadcast receiver in the manifest and define the action

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

The Client sends a broadcast

Intent intent4 = new Intent("server.ServerBroadCastReceiver"); // New componentName = new componentName ("com.jxx.server"."com.jxx.server.ServerBroadCastReceiver");
intent4.setComponent(componentName);                                                                        
sendBroadcast(intent4);                                                                                     
Copy the code

5. Service

In Android Service (2), we introduce how to implement IPC communication via Service. This can also be used to invoke an App.