ITeye博客

Android中如何做到Service被关闭后又自动启动

bolutes · 2011-12-20 10:22 · Markdown

转载:GL(arui319)http://blog.csdn.net/arui319 http://blog.csdn.net/arui319/article/details/7040980

那如何做到启动一个Service,并且在用户关闭后能自动又启动了呢?

一般的,都会在上面说到的BroadcastReceiver的实现里面,监听手机启动完成后,启动一个Service,这是一般的做法。问题是,用户可以关闭掉该Service。那么怎样才能使它被关闭掉以后,再次启动呢?聪明的你一定立即就想到了,如果不直接启动Service,而是启动一个timmer,或者alarmManager,然后每隔一段时间去启动Service,就可以了

备注一:这里面涉及的PendingIntent之前没有接触过
转载:http://www.devdiv.com/home.php?mod=space&uid=1&do=blog&id=2644

Intent比较简单,类似消息,发送给别的activity,别的activity会立即执行
我主要说说什么是PendingIntent以及它的执行过程
以alarm service为例:
1. activity请求一个alarm一般这样来做:
# //创建一个PendingIntent
# Intent intent = new Intent(ALARM_ALERT_ACTION);
# intent.putExtra(ID, id);
# intent.putExtra(TIME, atTimeInMillis);
# PendingIntent sender = PendingIntent.getBroadcast(
# context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
# //获得AlarmMnager并注册一个新闹铃,
# //一次性闹铃的设置
#
# AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
# am.set(AlarmManager.POWER_OFF_WAKEUP, atTimeInMillis, sender);
2. AlarmManager.set调用AlarmManagerService.set
3. AlarmManagerService.set的核心代码如下:

我们可以看到它就是把PendingIntent保存起来而已
4. alarm manager service会定时查看是否有alarm到期了,如果到期了做相应处理。
5. AlarmThread会调用
alarm.operation.send(mContext, 0,
mBackgroundIntent.putExtra(
Intent.EXTRA_ALARM_COUNT, alarm.count),
mResultReceiver, mHandler);
也就是通过PendingIntent.send执行intent操作,alarm这个就会发送ALARM_ALERT_ACTION的broadcast。

补充说明:
1. PendingIntent重要特点是异步处理。
2. 另外有一个要说明的是PendingIntent.onFinished,它可以作为PendingIntent.send的一个参数,
我们知道PendingIntent.send一般是在service中执行,这个调用的send后,回调onFished类的onSendFinished,所以onSendFinished一般也是在service中执行的

首先看官方解释:An Intent is something that is used right now; a PendingIntent is something that may create an Intent in the future. You will use a PendingIntent with Notifications, AlarmManager, etc.

PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情(By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)

备注二:AlarmManager

转载:http://www.cnblogs.com/jico/archive/2010/11/03/1868361.html

AlarmManager的使用机制有的称呼为全局定时器,有的称呼为闹钟。通过对它的使用,个人觉得叫全局定时器比较合适,其实它的作用和Timer有点相似。都有两种相似的用法:(1)在指定时长后执行某项操作(2)周期性的执行某项操作

AlarmManager对象配合Intent使用,可以定时的开启一个Activity,发送一个BroadCast,或者开启一个Service.

下面的代码详细的介绍了两种定时方式的使用:

(1)在指定时长后执行某项操作

代码 

       //操作:发送一个广播,广播接收后Toast提示定时操作完成

     Intent intent =new Intent(Main.this, alarmreceiver.class);
    intent.setAction("short");
    PendingIntent sender=
        PendingIntent.getBroadcast(Main.this, 0, intent, 0);

//设定一个五秒后的时间
    Calendar calendar=Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 5);

    AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
//或者以下面方式简化
    //alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5*1000, sender);
    
    Toast.makeText(Main.this, "五秒后alarm开启", Toast.LENGTH_LONG).show();

//注意:receiver记得在manifest.xml注册

代码



    public static class alarmreceiver extends BroadcastReceiver{

        @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
            if(intent.getAction().equals("short")){
                Toast.makeText(context, "short alarm", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(context, "repeating alarm", 

                      Toast.LENGTH_LONG).show();
            }
        }
    }

(2)周期性的执行某项操作

代码



    Intent intent =new Intent(Main.this, alarmreceiver.class);
    intent.setAction("repeating");
    PendingIntent sender=PendingIntent
        .getBroadcast(Main.this, 0, intent, 0);



    //开始时间
long firstime=SystemClock.elapsedRealtime();

    AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);


  //5秒一个周期,不停的发送广播
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP
            , firstime, 5*1000, sender);

AlarmManager的setRepeating()相当于Timer的Schedule(task,delay,peroid);有点差异的地方时Timer这个方法是指定延迟多长时间

以后开始周期性的执行task;

AlarmManager的取消:(其中需要注意的是取消的Intent必须与启动Intent保持绝对一致才能支持取消AlarmManager)

代码



  Intent intent =new Intent(Main.this, alarmreceiver.class);
  intent.setAction("repeating");
  PendingIntent sender=PendingIntent
         .getBroadcast(Main.this, 0, intent, 0);
  AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
alarm.cancel(sender);

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。