`

Android中Intent,service,broadcast应用浅析

 
阅读更多

 Android中Intent,service,broadcast应用浅析(一)

 典型的Android应用程序由两部分构成,一个是在前台运行的Activity和View,一个就是在后台运行的Intent 和Service对象,还有一种是是广播接收器,BroadCastReceiver,我们通常启动一个service(服务)对象或者发送一个广播,都是由Intent 来启动的.
 
首先来看下怎么用Intent来启动一个服务:
写了一个小例子,在主页面上有两个按钮,一个点击是启动服务,一个点击是取消服务,看了界面,再看一下界面,在看一下源代码的截图.

 

 

关于服务需要说明的是:服务中只有onCreate,onStart,和onStop方法,当第一次启动服务的时候调用的是onCreate,onStart方法,停止服务时调用onStop方法,完了之后再启动服务就只需要调用onStart方法了.

 

  1. public class Activity01 extends Activity {  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.main);  
  6.         Button startService = (Button) findViewById(R.id.startBtn);  
  7.         startService.setOnClickListener(new OnClickListener() {  
  8.             @Override  
  9.             public void onClick(View v) {  
  10.                 startService(new Intent(Activity01.this,  
  11.                         BackgroundService.class));  
  12.             }  
  13.         });  
  14.         Button stopService = (Button) findViewById(R.id.stopBtn);  
  15.         stopService.setOnClickListener(new OnClickListener() {  
  16.             @Override  
  17.             public void onClick(View v) {  
  18.                 stopService(new Intent(Activity01.this, BackgroundService.class));  
  19.             }  
  20.         });  
  21.     }  

 

我现在写的这个小服务的功能是满足时间条件后刷新状态栏,具体的说,就是启动服务之后开始计算时间,当时间过了一定的时间点之后就刷新状态栏,因为之前要在程序中做这一块,就写了这样的一个小例子.
 
先看代码中onCreate方法,声明了一个通知栏管理的对象,然后用了一个handler,这个handler收到message之后静态变量seconds加一,然后更新状态栏。

 

  1. @Override  
  2.     public void onCreate() {  
  3.         super.onCreate();  
  4.         notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  5.         handler = new Handler() {  
  6.             public void handleMessage(Message msg) {  
  7.                 switch (msg.what) {  
  8.                 case 1:  
  9.                     seconds++;  
  10.                     updateNotification(seconds);  
  11.                     break;  
  12.                 }  
  13.                 super.handleMessage(msg);  
  14.             }  
  15.         };  
  16.         timer = new Timer(false);  
  17.     }  

 

但是handler是怎样收到message的呢?这是我们在onStart中创建了一个timer的对象,可以看代码,onStart方法中向通知栏发送一个消息,说明已经启动服务,然后调用run方法,每个一秒钟向handler发送一个消息.handler接收到消息后执行的代码已经在onCreate中说过

 

  1. @Override  
  2. public void onStart(Intent intent, int startId) {  
  3.     super.onStart(intent, startId);  
  4.     displayNotificationMessage("starting Background Service");  
  5.     timer.schedule(new TimerTask() {  
  6.         @Override  
  7.         public void run() {  
  8.             Message message = new Message();  
  9.             message.what = 1;  
  10.             handler.sendMessage(message);  
  11.         }  
  12.     }, 1000, 1000);  

 

当我去停止服务的时候,向通知栏中发动一个消息,说明停止服务,然后重新置标记服务已经开始了多长时间的变量为0,同时用timer.cancel()方法来停止timer的运行。
 

 

  1. @Override  
  2.     public void onDestroy() {  
  3.         super.onDestroy();  
  4.         displayNotificationMessage("stopping Background Service");  
  5.         seconds = 0;  
  6.         timer.cancel();  
  7.  
  8.     } 

 

向状态栏发送信息的方法,需要说明的就是这个PendingIntent,刚开始的时候很不理解这个东西,后来终于搞明白了,Intent是发送出去一个任务,我们向状态栏中发送一个消息,状态栏下拉点击的时候我们要让他有什么样的反应,就是需要用PendingIntent来定义,相当于PendingIntent定义的是这个操作被触发的时候需要的指示操作,通俗理解,Intent相当于你跟别人发送一个直接命令,说让别人直接做什么.PendingIntent相当于你给别人一个命令,命令中告诉他当有紧急事件发生的时候做什么,总之有点难理解了.

 

  1. private void displayNotificationMessage(String message) {  
  2.     Notification notification = new Notification(R.drawable.icon, message,  
  3.             System.currentTimeMillis());  
  4.     PendingIntent contentIntent = PendingIntent.getActivity(this, 0,  
  5.             new Intent(this, Activity01.class), 0);  
  6.       
  7.     notification.setLatestEventInfo(this, "Background Service", message,  
  8.             contentIntent);  
  9.     notificationMgr.notify(R.id.app_notification_id, notification);  
  10. }  

 

本文出自 “HDDevTeam” 博客,http://hddev.blog.51cto.com/3365350/622627

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics