Android实现Activity、Service与Broadcaster三大组件之间互相调用的方法

2019-12-10 18:45:10丽君
易采站长站为您分析Android实现Activity、Service与Broadcaster三大组件之间互相调用的方法,结合实例形式详细分析了Activity、Service与Broadcaster三大组件相互调用的原理,实现方法与相关注意事项,需要的朋友可以参考下  

本文实例讲述了Android实现Activity、Service与Broadcaster三大组件之间互相调用的方法。,具体如下:

我们研究两个问题,

1、Service如何通过Broadcaster更改activity的一个TextView。
(研究这个问题,考虑到Service从服务器端获得消息之后,将msg返回给activity)

2、Activity如何通过Binder调用Service的一个方法。
(研究这个问题,考虑到与服务器端交互的动作,打包至Service,Activity只呈现界面,调用Service的方法)

结构图见如下:

Android,Activity,Service,Broadcaster

效果图如下:

Android,Activity,Service,Broadcaster

点击“start service”按钮,启动Service,然后更改Activity的UI。

Android,Activity,Service,Broadcaster

点击“send msg to server”按钮调用Service的方法,显示NotificationBar

代码:

1、新建一个MyService类,继承Service

package com.ljq.activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
 private NotificationManager notificationManager = null;
 private final IBinder binder = new LocalBinder();
 @Override
 public void onCreate() {
 sendMsgtoActivty("Service is oncreating.n");
 }
 @Override
 public IBinder onBind(Intent intent) {
 String msg = "Activity is sendding message to service,n Service send msg to server!n";
 sendMsgtoActivty(msg);
 return binder;
 }
 /**
 * 把信息传递给activity
 *
 * @param msg
 */
 private void sendMsgtoActivty(String msg) {
 Intent intent = new Intent("com.android.Yao.msg");
 intent.putExtra("msg", msg);
 this.sendBroadcast(intent);
 }
 @Override
 public void onDestroy() {
 super.onDestroy();
 if(notificationManager!=null){
  notificationManager.cancel(0);
  notificationManager=null;
 }
 }
 /**
 * 在状态栏显示通知
 *
 * @param msg
 */
 private void showNotification(String msg) {
 notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 // 定义Notification的各种属性
 Notification notification =new Notification(R.drawable.icon,
     "A Message Coming!", System.currentTimeMillis());
 //FLAG_AUTO_CANCEL  该通知能被状态栏的清除按钮给清除掉
 //FLAG_NO_CLEAR   该通知不能被状态栏的清除按钮给清除掉
 //FLAG_ONGOING_EVENT 通知放置在正在运行
 //FLAG_INSISTENT   是否一直进行,比如音乐一直播放,知道用户响应
 notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
 notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
 notification.flags |= Notification.FLAG_SHOW_LIGHTS;
 //DEFAULT_ALL   使用所有默认值,比如声音,震动,闪屏等等
 //DEFAULT_LIGHTS 使用默认闪光提示
 //DEFAULT_SOUNDS 使用默认提示声音
 //DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE" />权限
 notification.defaults = Notification.DEFAULT_LIGHTS;
 //叠加效果常量
 //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
 notification.ledARGB = Color.BLUE;
 notification.ledOnMS =5000; //闪光时间,毫秒
 // 设置通知的事件消息
 //Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 点击该通知后要跳转的Activity
 Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); // 加载类,如果直接通过类名,会在点击时重新加载页面,无法恢复最后页面状态。
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
 PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
 notification.setLatestEventInfo(this, "Message", "Message:" + msg, contentItent);
 // 把Notification传递给NotificationManager
 notificationManager.notify(0, notification);
 }
 /**
 * 从activity获取信息
 *
 * @param msg
 */
 public void receiverMsgtoActivity(String msg){
 sendMsgtoActivty("n receiverMsgtoActivity:"+msg);
 }
 public void sendMsgtoServer(String msg) {
 showNotification(msg);
 }
 public class LocalBinder extends Binder {
 public MyService getService() {
  return MyService.this;
 }
 }
}