1、AIDL
2、BroadCast
3、Activity
BroadCast是被动跨进程通信,只能被动接收访问。
实际开发中常用来做什么?
1.监听短信,监听来电,监听网络。
2.可以增强APP之间的互动,和用户粘性。不过个人认为这个很没必要,增加粘性,最简单的方法是推送
举个栗子:两个程序:A程序和B程序,A发送广播,B接收广播。
1.新建A程序,随意写一个点击事件,向B发送广播
Intent intent = new Intent("com.wgl.defaultBroadcast");
MainActivity.this.sendBroadcast(intent);
注意:参数"com.wgl.defaultBroadCast"对应B程序的A程序的清单配置文件manifest,不需要任何权限。
安装A程序。
2.新建B程序,新建类,起个名字:AcceptBroadCast,继承BroadCastReceiver
/**
* 自定义广播
* Author:Biligle.
*/
public class AcceptReciver extends BroadcastReceiver {
private static String ACTION = "com.wgl.defaultBroadcast";
@Override
public void onReceive(Context context, Intent intent) {
//intent.getAction():获取发送过来的action)(从A发送过来的)
if(intent.getAction().equals(ACTION)){
Toast.makeText(context,"接到一个广播!!!",Toast.LENGTH_LONG).show();
}
}
}
manifest文件:
注意效果图:
如果总觉得Toast出一个广播内容,有点儿low,那么,接下来加入Service+Notification,后台提示,好像有点儿推送的意思了。
上干货:
A程序不动,修改B程序,如下:
1.新建BackgroundService继承Service
appName:项目的名字。
data:点击后台消息后,跨进程跳转B程序,Intent intent = new Intent(action,Uri.parse(data))。
public class BackgroundService extends Service {
private static final int DOWN_OK = 1;
private String app_name;
private String data;
private NotificationManager notificationManager;
private Notification notification;
private RemoteViews contentView;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
/**
* 方法描述:onStartCommand方法
* @param intent
* @param flags
* @param startId
* @return
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
app_name = intent.getStringExtra("appName");
data = intent.getStringExtra("data");
createNotification();
return super.onStartCommand(intent, flags, startId);
}
/**
* 方法描述:createNotification方法
*/
@SuppressWarnings("deprecation")
public void createNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification = new Notification.Builder(BackgroundService.this)
.setAutoCancel(true)
.setContentTitle(app_name + "点击打开")
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
}else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
Notification.Builder builder = new Notification.Builder(BackgroundService.this)
.setAutoCancel(true)
.setContentTitle(app_name + "点击打开")
.setContentText("describe")
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setOngoing(true);
notification=builder.getNotification();
}
notification.flags = Notification.FLAG_ONGOING_EVENT;
/*** 自定义 Notification 的显示****/
contentView = new RemoteViews(getPackageName(),R.layout.notification_item);
contentView.setTextViewText(R.id.notificationTitle, app_name + "点击打开");
notification.contentView = contentView;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
//给点击事件加入ID标识,方便跳转之后删除通知
intent.putExtra("notifycationId",R.id.notificationTitle);
//必须开启一个新栈
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//三个参数请自行百度
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
1,
intent, PendingIntent.FLAG_CANCEL_CURRENT
);
//为单独按钮添加点击事件
contentView.setOnClickPendingIntent(R.id.notificationTitle,pendingIntent);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//注意这里的R.id.notificationTitle要和intent.putExtra的参数一致
notificationManager.notify(R.id.notificationTitle, notification);
}
}
2.在AcceptReceiver里面,这样修改:
/**
* 自定义广播
* Author:Biligle.
*/
public class AcceptReciver extends BroadcastReceiver {
private static String ACTION = "com.wgl.defaultBroadcast";
@Override
public void onReceive(Context context, Intent intent) {
//intent.getAction():获取发送过来的action)(从A发送过来的)
if(intent.getAction().equals(ACTION)){
// Toast.makeText(context,"接到一个广播!!!", Toast.LENGTH_LONG).show();
Intent i = new Intent(context, BackgroundService.class);
i.putExtra("appName", "跨进程");
i.putExtra("data", "content://com.wgl.share:8080/openApp");
context.startService(i);//开启后台服务
}
}
}
3.在B程序的OpenActivity里面
public class OpenActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
int notifycationId = getIntent().getIntExtra("notifycationId",0);
if(notifycationId != 0){
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notifycationId);
}
}
}