Several ways of APP mutual Awakening

Time:2021-6-8

In the following, the client is used to represent the operating app, the server is used to represent the remote app to be aroused, and the package name of the server is “com. Jxx. Server”

1. ComponentName

Using componentname to call up the server is very simple. It should be noted that the activity of the server needs to set exported to true in the manifest configuration

The configuration of the server is as follows:


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

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

There is another way to add componentname to intent


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

It’s just that setclassname helps us to create an instance of new componentname


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

Since you are using intent to call up activities, you can use the features of intent, such as using bundles to transfer 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);

It’s also easy to extract data on the server side


Bundle bundle = getIntent().getExtras();

2. Implicit jump, URI

This is what the Android dial-up page looks like


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

In fact, we call up the server in the form of URI and transfer the data. Let’s implement it ourselves. In this way, the configuration of the server side 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>   

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

What do we see in URI? It’s followed by “remote.”_ invoke=from_ This is actually used to pass data to the server. We can parse it in the server


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

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 to let us choose which page to jump to when the page jumps, as shown in the figure below:

 

3. Call through the package manager

Just know the package name of the server


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

4. Static broadcast receiver

The server only needs to register a static broadcast receiver and jump to activity in the broadcast receiver. The client only needs to send a broadcast.

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,
  //The reason is that the context here is not an activity type context, and the activity cannot be opened directly
  intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(intent1);
 }
}

And register as the 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>

Just send the broadcast in the client

Intent intent4 = new Intent("server.ServerBroadCastReceiver"); 
//Add componentname here to solve problems that cannot be aroused above 8.0          
ComponentName componentName = new ComponentName("com.jxx.server", "com.jxx.server.ServerBroadCastReceiver");
intent4.setComponent(componentName);                  
sendBroadcast(intent4);

5. Service

In the detailed explanation of Android service (2), we introduce how to realize IPC communication through service, which can also be used to arouse app. There is no more introduction here. Students who are interested can click to view it.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

What is “hybrid cloud”?

In this paper, we define the concept of “hybrid cloud”, explain four different cloud deployment models of hybrid cloud, and deeply analyze the industrial trend of hybrid cloud through a series of data and charts. 01 introduction Hybrid cloud is a computing environment that integrates multiple platforms and data centers. Generally speaking, hybrid cloud is […]