码迷,mamicode.com
首页 > 移动开发 > 详细

android 后台服务定时通知

时间:2014-10-09 23:27:57      阅读:13388      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   color   io   os   使用   

   最近有个项目的要求是在程序退出之后,任然可以每天定时发通知,我们可以想下,其实就是后台开一个服务,然后时间到了就发下通知。

1.首先我们需要用到Service类。

先上代码在慢慢解释

 1 package com.example.androidnotification;
 2 
 3 import java.util.Timer;
 4 import java.util.TimerTask;
 5 import android.app.Notification;
 6 import android.app.NotificationManager;
 7 import android.app.PendingIntent;
 8 import android.app.Service;
 9 import android.content.Intent;
10 import android.os.IBinder;
11 import android.util.Log;
12 
13 public class PushService extends Service {
14 
15      static Timer timer = null;
16     //清除通知
17     public static void cleanAllNotification() {
18         NotificationManager mn= (NotificationManager) MainActivity.getContext().getSystemService(NOTIFICATION_SERVICE);
19         mn.cancelAll();    
20         if (timer != null) {
21             timer.cancel();
22             timer = null;
23         }
24     }
25 
26     //添加通知
27     public static void addNotification(int delayTime,String tickerText,String contentTitle,String contentText)
28     {            
29         Intent intent = new Intent(MainActivity.getContext(), PushService.class);
30         intent.putExtra("delayTime", delayTime);
31         intent.putExtra("tickerText", tickerText);
32         intent.putExtra("contentTitle", contentTitle);
33         intent.putExtra("contentText", contentText);       
34         MainActivity.getContext().startService(intent);
35     }
36     
37     public void onCreate() {
38         Log.e("addNotification", "===========create=======");
39     }
40     
41     @Override
42     public IBinder onBind(Intent arg0) {
43         // TODO Auto-generated method stub
44         return null;
45     }
46 
47     public int onStartCommand(final Intent intent, int flags, int startId) {
48             
49         long period = 24*60*60*1000; //24小时一个周期
50         int delay=intent.getIntExtra("delayTime",0);
51         if (null == timer ) {
52             timer = new Timer();
53         }
54         timer.schedule(new TimerTask() {
55             
56             @Override
57             public void run() {
58                 // TODO Auto-generated method stub
59                 NotificationManager mn= (NotificationManager) PushService.this.getSystemService(NOTIFICATION_SERVICE);    
60                 Notification.Builder builder = new Notification.Builder(PushService.this);
61                 Intent notificationIntent = new Intent(PushService.this,MainActivity.class);//点击跳转位置                
62                 PendingIntent contentIntent = PendingIntent.getActivity(PushService.this,0,notificationIntent,0);                
63                 builder.setContentIntent(contentIntent);
64                 builder.setSmallIcon(R.drawable.ic_launcher);
65                 builder.setTicker(intent.getStringExtra("tickerText")); //测试通知栏标题
66                 builder.setContentText(intent.getStringExtra("contentText")); //下拉通知啦内容
67                 builder.setContentTitle(intent.getStringExtra("contentTitle"));//下拉通知栏标题
68                 builder.setAutoCancel(true);
69                 builder.setDefaults(Notification.DEFAULT_ALL);
70                 Notification notification = builder.build();
71                 mn.notify((int)System.currentTimeMillis(),notification);
72             }
73         },delay, period);
74                 
75         return super.onStartCommand(intent, flags, startId);
76     }
77     
78     @Override
79     public void onDestroy(){
80         Log.e("addNotification", "===========destroy=======");
81        super.onDestroy();
82     }
83 }

自定义了一个类PushService继续Service,定义了两个类来实现添加通知和取消通知

//delayTime 延迟多久执行。

//tickerText 

//contentTitle 通知栏的标题

//contentText 通知栏的内容

addNotification(int delayTime,String tickerText,String contentTitle,String contentText)

 

//清除通知

cleanAllNotification()

====================================

Service的启动,startService来启动服务只执行一次onCreate方法,但是每次调用一次startService就会执行一次onStartCommand函数。

2.注册服务类

在AndroidManifest.xml中的application字段中加入如下信息来注册这个服务类。

<service android:enabled="true" android:name=".PushService" android:process="system">
        </service>

这边有一点非常重要的是 android:process="system" ,设置为system,否则按退出键使用如下方式来执行会导致程序崩溃

android.os.Process.killProcess(android.os.Process.myPid());或者System.exit(0)

而且服务也会被终止,因为service是和主线程在一起的,主线程被终止了,服务线程也会停止掉,就无法在后台执行了,所以我们必须把服务注册到系统中

工程源码

android 后台服务定时通知

标签:des   android   style   blog   http   color   io   os   使用   

原文地址:http://www.cnblogs.com/chuanwei-zhang/p/4014390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!