带唤醒锁的报警管理器服务

时间:2014-04-23 07:02:06

标签: android alarmmanager

我正在使用AlarmManager来安排我的应用中的内容,用户将选择时间,然后我将服务等级的intent传递给AlarmManager,这将触发屏幕打开后的一定时间后发出警报。

它正常工作但屏幕锁定时,不会触发警报。

我在服务中使用 wakelock 部分唤醒锁定选项,但它无效。当我使用完全唤醒锁然后它正常工作,那么部分锁定选项有什么问题?

下面的代码。

public void schedule(View v) {
    AlarmManager localAlarmManager = (AlarmManager)getSystemService("alarm");
    Calendar localCalendar = Calendar.getInstance();

    localCalendar.set(Calendar.HOUR_OF_DAY, 12);
    localCalendar.set(Calendar.MINUTE, 10);
    localCalendar.set(Calendar.SECOND, 0);
    Intent localIntent = new Intent(getBaseContext(), Backupservice.class);
    localIntent.putExtra("startservice", "true");

    PendingIntent localPendingIntent = PendingIntent.getService(getBaseContext(), 15, localIntent, 134217728);
    localAlarmManager.cancel(localPendingIntent);
    long l = localCalendar.getTimeInMillis();

    System.out.println("schtm:" + localCalendar.getTimeInMillis() +"currenttm:"+System.currentTimeMillis());

    localAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, l,1800000, localPendingIntent);
}



public class Backupservice extends Service {

    public Backupservice(){
        // cnt=context;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        miscallsettings=getSharedPreferences("MyPref", MODE_PRIVATE);
        Log.i("Backupservice", "Service created");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        super.onStartCommand(intent, flags, startId);
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

        //  PowerManager.ACQUIRE_CAUSES_WAKEUP |
        wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK   |
               PowerManager.ON_AFTER_RELEASE, "ggg");
        wl.acquire();
        save();
        return super.onStartCommand(intent, flags, startId);
    }

    @SuppressWarnings("unchecked")
    private void save() {
        try {
            // here I am writing the logic
            wl.release();
        } catch(Exception e) {

        }
    }

    @Override
    public void onDestroy() {
        try {
            wl.release();   
        } catch(Exception e) {

        }
    }
}

3 个答案:

答案 0 :(得分:2)

我已经看到这种实现的方式,它对我有用,就是使用BroadcastReceiver作为警报事件。只要方法运行,此广播接收器的onReceive方法就可以保证唤醒设备。

我不确定这是不是最好的方式,但我的情况类似,这是我所要做的:

在schedule方法中设置闹钟以发送警报广播意图:

AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent alarmBcstRec = new Intent(context, MyAlarmBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, alarmBcstRec, 0);
// Replace the settings with yours here...
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+MyAlarmBroadcastReceiver.INTERVAL_FIVE_MINUTES, MyAlarmBroadcastReceiver.INTERVAL_FIVE_MINUTES, pi);

启动服务的警报广播接收器。顺便说一句,在服务的onStartCommand方法中我获取了一个唤醒锁,我不确定它是否适合这样做,但它对我有用。我在服务完成后发布它。

public class MyAlarmBroadcastReceiver extends BroadcastReceiver {

public static final long INTERVAL_FIVE_MINUTES = 1000 * 60 * 5;

@Override
public void onReceive(Context context, Intent intent) {

    Intent service = new Intent(context, YourService.class);

    context.startService(service);
}

}

在我添加的Manifest中:

    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <receiver android:name="com.xxx.MyAlarmBroadcastReceiver"
        android:enabled="true">
    </receiver>

在我的情况下,我还有一个BroadcastReceiver BOOT_COMPLETE来启动系统启动时的调度。在这种情况下,我从Boot广播接收器设置了与调度方法所示相同的代码(将警报设置为广播到MyAlarmBroadCastReceiver)。

答案 1 :(得分:0)

检查我的代码:

   AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
   PendingIntent pendingIntent = getNotifyPendingIntent();
   alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
     pendingIntent);



   private PendingIntent getNotifyPendingIntent() {
    Intent intent = new Intent(context, AlarmExpireService.class);
    return PendingIntent.getService(context, 0, intent, 0);
}

现在AlarmExpireService是:

public class AlarmExpireService extends Service {

// private static final String TAG = "AlarmExpireService";
private static final String TAG = "AlarmExpireService";

@Override
public void onCreate() {
    super.onCreate();
    Intent intent = new Intent(this, AlarmAlert.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    startActivity(intent);
    // generateNotification(this, "Hello");
    // Toast.makeText(this, "Alarm ring", Toast.LENGTH_LONG).show();
    stopSelf();
}
}

当屏幕锁定时,此标志对于trigeer活动非常有用。

答案 2 :(得分:0)

尝试在后台应用管理设置中允许您的应用。

相关问题