安卓ApiDemos学习 app/Alarm/AlarmService
来源:互联网 发布:姆潘巴现象 知乎 编辑:程序博客网 时间:2024/05/17 04:39
使用alarm机制定时启动service,并让service显示Notification的例子
首先创建待定意图
- mAlarmSender = PendingIntent.getService(AlarmService.this,0, new Intent(AlarmService.this, AlarmService_Service.class), 0);
- AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
- am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, 30*1000, mAlarmSender);
AlarmService_Service是接收intent的service
service首先显示Notification
- mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- showNotification();
- private void showNotification() {
- CharSequence text = getText(R.string.alarm_service_started);
- Notification notification = new Notification(R.drawable.stat_sample, text,
- System.currentTimeMillis());
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
- new Intent(this, AlarmService.class), 0);
- notification.setLatestEventInfo(this, getText(R.string.alarm_service_label),
- text, contentIntent);
- mNM.notify(R.string.alarm_service_started, notification);
- }
Notification的第1个参数,用来表示通知发出时,状态栏显示的的图标
Notification的第2个参数,用来表示通知发出时,状态栏显示的滚动文字
Notification的第3个参数,用来表示通知的时间
notification.setLatestEventInfo的参数2,里面设置的文字对应Sample Alarm Service这句话
notification.setLatestEventInfo的参数3,里面设置的文字对应The alarm service has started running这句话
- mNM.notify(R.string.alarm_service_started, notification);
service退出的时候,就会将通知消除
- public void onDestroy() {
- // Cancel the notification -- we use the same ID that we had used to start it
- mNM.cancel(R.string.alarm_service_started);
- // Tell the user we stopped.
- Toast.makeText(this, R.string.alarm_service_finished, Toast.LENGTH_SHORT).show();
- }
点击通知的时候,会执行传入的待定意图,启动画面
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
- new Intent(this, AlarmService.class), 0);
- notification.setLatestEventInfo(this, getText(R.string.alarm_service_label),
- text, contentIntent);
注意:PendingIntent.getService是用来启动service的待定意图,PendingIntent.getActivity是用来启动Activity的待定意图
service接着启动一个线程,用来暂停15秒
- Thread thr = new Thread(null, mTask, "AlarmService_Service");
- thr.start();
- long endTime = System.currentTimeMillis() + 15*1000;
- while (System.currentTimeMillis() < endTime) {
- synchronized (mBinder) {
- try {
- mBinder.wait(endTime - System.currentTimeMillis());
- } catch (Exception e) {
- }
- }
- }
- // Done with our work... stop the service!
- AlarmService_Service.this.stopSelf();
最后分析下Binder运用。按照我的理解,Binder就像是service提供的一个远程调用的插座。
其他进程可以通过这个插座连接到service上,并调用Binder提供的一些方法。ServiceConnection就像是其他进程的插头,用来取得service提供的Binder。
- private ServiceConnection mConnection = new ServiceConnection(){
- public void onServiceConnected(ComponentName className, IBinder service) {
- // Called when the connection is made.
- serviceBinder = ((MyService.MyBinder)service).getService();
- }
- public void onServiceDisconnected(ComponentName className) {
- // Received when the service unexpectedly disconnects.
- serviceBinder = null;
- }
- };
- Intent bindIntent = new Intent(MyActivity.this, MyService.class);
- bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);
因为没有人调用binder的notify,所以它只能老老实实的等待wait函数超时了
- 安卓ApiDemos学习 app/Alarm/AlarmService
- 安卓ApiDemos学习 app/Alarm/AlarmService
- 安卓ApiDemos学习 app/Alarm/AlarmController
- 安卓ApiDemos学习 app/Activity/Animation
- 安卓ApiDemos学习 app/Activity/Forwarding
- 安卓ApiDemos学习 app/Activity/HelloWorld
- 安卓ApiDemos学习 app/Activity/PersistentState
- 安卓ApiDemos学习 app/Activity/QuickContactsDemo
- 安卓ApiDemos学习 app/Activity/ReceiveResult
- 安卓ApiDemos学习 app/Activity/Redirection
- 安卓ApiDemos学习 app/Activity/ReorderActivities
- 安卓ApiDemos学习 app/Activity/SetWallpaper
- 安卓ApiDemos学习 app/Activity/Wallpaper
- 安卓ApiDemos学习 app/Dialog
- 安卓ApiDemos学习 app/Activity/Translucent Blur
- 安卓ApiDemos学习 app/Device Admin/DeviceAdminSample
- Android ApiDemos示例解析(19):App->Alarm->Alarm Controller
- Android ApiDemos示例解析(20):App->Alarm->Alarm Service
- ListView.setOnItemClickListener无效
- linux硬链接与软链接
- sizeof()和lstrlen()和strlen()区别
- 程序员都该看的剧——《恋爱的犀牛》剧评
- DG6—— 用RMAN复制 搭建 物理 Data Gurad 环境
- 安卓ApiDemos学习 app/Alarm/AlarmService
- ServiceMix中文教程
- 男人的标准
- Android上面的dp单位
- 十种主要排序算法比较
- CreateFile打开U盘的方法1-逻辑方式
- 七夕,爱似流年
- 深入浅出 Cocoa 多线程编程之 block 与 dispatch quene
- 安装mysql 获得 mysql.h 建立C接口