android 里的各种通信方式
这几天 想把 Android里的通信方式 做一些总结。我一步步来吧。从最简单的开始。由于经验有限,有不妥之处,还要多多包涵。
一:intent 传递方式(包括activity之间,activity到service等等)
Activity它们之间就会存在相互转跳的关系
转跳的实现方式还是使用Intent 然后startActivity ,当然转跳的话是可以带数据过去的。比如从A跳到B 可以把A中的一些数据通过Intent传递给B 。
这里,传递 数据比较简单了。
- Intent(mContext,ShowActivity.);
- , );
- Bundle bundle = Bundle();
- , );
- bundle.putInt(, );
-
Bundle.putSerializable(Key,Object); //实现Serializable接口的对象 Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象Bundle bundle = new Bundle(); bundle.putSerializable("user", user); intent.putExtras(bundle); this.startActivity(intent);二:Fragment之间的通信方式
Fragment之间传递参数通信就相对于activity而言,就差多了。但是他们可以 通过他们 宿主 activity来通信,比如要调用某一个Fragment的方法,则想办法从宿主activity去调用另一个Fragment的方法。再不行,还可以通过广播,不建议 直接去调用另一个Fragment的方法。
三:UI线程和子线程之间的通信
handler 大家可以把它想象成主线程和子线程的一个通信处理类,它可以给主线程(UI线程)发送数据从而更新主线程(UI线程)的UI与逻辑。
// 定时器 线程
class ChangeTextThread implements Runnable{
@Override
public void run() {
synchronized (broadCastTextView) {
while (true) {
sCount++;
if(sCount>=contentList.size()){
sCount=0;
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
broadCastTextView.next();
broadCastTextView.setText(contentList.get(sCount));
}
});
}}} }
四:notifation 通知栏
Notifation通知栏会在屏幕上方向用户提示信息 但是不会打断用户正在阅读的内容,除非用户手动将 Notifation通知栏拉下。 Notifation的好处就是在于不会影响用户的操作,比如用户正在阅读非常重要的信息这时候帮他直接打开一个activity会非常不合适 因为直接影响到了他当时的操作行为 所以Notifation就出来了。建议大家在开发中遇到可能打断用户使用的情况下都去使用Notifation通知栏。
- public NotificationActivity Activity {
- NotificationManager mManager = ;
- ;
- onCreate(Bundle savedInstanceState) {
- setContentView(R.layout.notification);
- Notification(R.drawable.jay,
- , System.currentTimeMillis());
- Intent intent = Intent(, MyShowActivity.);
- Bundle bundle = Bundle();
- , );
- intent.putExtras(bundle);
- ,
- R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- , ,
- OnClickListener() {
- onClick(View arg0) {
- , notification);
- }
- OnClickListener() {
- onClick(View arg0) {
- .onCreate(savedInstanceState);
- }
- }
五:广播
Android开发中如果须要对两个完全没关系的程序之间进行通信 就可以使用发送广播与接收广播的机制来实现 ,例如程序A发送了一个广播 程序B接受到 做一些事情 这样就达到了相互的通讯。
public BroadcastActivity Activity { - ;
- Button mButton1 = ;
- onCreate(Bundle savedInstanceState) {
- setContentView(R.layout.broadcast);
- OnClickListener() {
- onClick(View arg0) {
- Intent(MyService.SEND_OK_MESSAGE);
- intent.putExtra(, );
- OnClickListener() {
- onClick(View arg0) {
- Intent intent = Intent(MyService.SEND_CANCLE_MESSAGE);
- , );
- sendBroadcast(intent);
- Intent(, MyService.);
- startService(i);
- .onCreate(savedInstanceState);
- }
- }
-
接收广播的话 我们开启一个service 在service中通过BroadcastReceiver 来接收广播 前提是须要接收的广播须要在onStart()中注册一下 在AndroidManifest.xml中可以过滤只接收须要接收的广播、 - >
- ></action>
- />
- <action android:name= />
- public MyService Service {
- String SEND_OK_MESSAGE = ;
- String SEND_CANCLE_MESSAGE = ;
- BroadcastReceiver myBroadCast = BroadcastReceiver() {
- onReceive(Context context, Intent intent) {
- (action.equals(SEND_OK_MESSAGE)) {
- + SEND_OK_MESSAGE, Toast.LENGTH_LONG).show();
- } (action.equals(SEND_CANCLE_MESSAGE)) {
- + SEND_CANCLE_MESSAGE, Toast.LENGTH_LONG).show();
- }
- onCreate() {
- .onCreate();
- }
- onStart(Intent intent, startId) {
- IntentFilter();
- myFilter.addAction(SEND_OK_MESSAGE);
- .registerReceiver(myBroadCast, myFilter);
- .onStart(intent, startId);
- }
- IBinder onBind(Intent arg0) {
- ;
- }
- >
- <intent-filter>
- />
- </intent-filter>
- />
- public MyBootReceiver BroadcastReceiver {
- String BOOT_COMPLETED = ;
- onReceive(Context context, Intent intent) {
- (intent.getAction().equals(BOOT_COMPLETED)) {
- Intent(context, MyService.);
- context.startService(i);
-
}

浙公网安备 33010602011771号