当前位置:操作系统 > 安卓/Android >>

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

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

 
关于服务需要说明的是:服务中只有onCreate,onStart,和onStop方法,当第一次启动服务的时候调用的是onCreate,onStart方法,停止服务时调用onStop方法,完了之后再启动服务就只需要调用onStart方法了.
 
public class Activity01 extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        Button startService = (Button) findViewById(R.id.startBtn); 
        startService.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                startService(new Intent(Activity01.this, 
                        BackgroundService.class)); 
            } 
        }); 
        Button stopService = (Button) findViewById(R.id.stopBtn); 
        stopService.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                stopService(new Intent(Activity01.this, BackgroundService.class)); 
            } 
        }); 
    } 
}
 
我现在写的这个小服务的功能是满足时间条件后刷新状态栏,具体的说,就是启动服务之后开始计算时间,当时间过了一定的时间点之后就刷新状态栏,因为之前要在程序中做这一块,就写了这样的一个小例子.
 
先看代码中onCreate方法,声明了一个通知栏管理的对象,然后用了一个handler,这个handler收到message之后静态变量seconds加一,然后更新状态栏。
 
@Override 
    public void onCreate() { 
        super.onCreate(); 
        notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
        handler = new Handler() { 
            public void handleMessage(Message msg) { 
                switch (msg.what) { 
                case 1: 
                    seconds++; 
                    updateNotification(seconds); 
                    break; 
                } 
                super.handleMessage(msg); 
            } 
        }; 
        timer = new Timer(false); 
    } 
 
但是handler是怎样收到message的呢?这是我们在onStart中创建了一个timer的对象,可以看代码,onStart方法中向通知栏发送一个消息,说明已经启动服务,然后调用run方法,每个一秒钟向handler发送一个消息.handler接收到消息后执行的代码已经在onCreate中说过
 
@Override 
public void onStart(Intent intent, int startId) { 
    super.onStart(intent, startId); 
    displayNotificationMessage("starting Background Service"); 
    timer.schedule(new TimerTask() { 
        @Override 
        public void run() { 
            Message message = new Message(); 
            message.what = 1; 
            handler.sendMessage(message); 
        } 
    }, 1000, 1000); 
}
 
当我去停止服务的时候,向通知栏中发动一个消息,说明停止服务,然后重新置标记服务已经开始了多长时间的变量为0,同时用timer.cancel()方法来停止timer的运行。
 
 
@Override 
    public void onDestroy() { 
        super.onDestroy(); 
        displayNotificationMessage("stopping Background Service"); 
        seconds = 0; 
        timer.cancel(); 
 
    }
 
向状态栏发送信息的方法,需要说明的就是这个PendingIntent,刚开始的时候很不理解这个东西,后来终于搞明白了,Intent是发送出去一个任务,我们向状态栏中发送一个消息,状态栏下拉点击的时候我们要让他有什么样的反应,就是需要用PendingIntent来定义,相当于PendingIntent定义的是这个操作被触发的时候需要的指示操作,通俗理解,Intent相当于你跟别人发送一个直接命令,说让别人直接做什么.PendingIntent相当于你给别人一个命令,命令中告诉他当有紧急事件发生的时候做什么,总之有点难理解了.
 
private void displayNotificationMessage(String message) { 
    Notification notification = new Notification(R.drawable.icon, message, 
            System.currentTimeMillis()); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
            new Intent(this, Activity01.class), 0); 
     
    notification.setLatestEventInfo(this, "Background Service", message, 
            contentIntent); 
    notificationMgr.notify(R.id.app_notification_id, n

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,