【问题标题】:alarm manager service with wake lock带唤醒锁的警报管理器服务
【发布时间】:2017-07-24 00:34:13
【问题描述】:

我正在使用AlarmManager 在我的应用程序中安排事情,用户将在其中选择时间,我将我的服务类的待处理intent 传递给AlarmManager,这应该会在屏幕显示特定时间后触发警报已开启。

可以正常使用,但是锁屏时不触发报警。

我在我的服务中使用了 wakelockpartial wake lock 选项,但它不起作用。当我使用完全唤醒锁时它可以正常工作,那么部分锁定选项有什么问题?

代码贴在下面。

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) {

        }
    }
}

【问题讨论】:

  • 显示您的 Backupservice.java 文件(您开始警报活动的位置)。
  • 对不起,我没听明白。如果你问的是 Backupservice 课程,我已经发布了
  • 我是说让我开始关注警报警报活动的意图。
  • 检查这个意图 localIntent = new Intent(getBaseContext(), Backupservice.class);在时间表()中
  • 你应该在调用服务时添加唤醒锁。这是一篇关于如何使用 Service 和 AlarmManager 的 blob 帖子:Using IntentService With AlarmManager to Schedule Alarms

标签: android alarmmanager


【解决方案1】:

我看到它的实现方式和对我有用的方式是使用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,以便在系统启动时启动调度。在这种情况下,从引导广播接收器中,我设置了与调度方法所示相同的代码(将警报设置为广播到MyAlarmBroadCastReceiver)。

【讨论】:

    【解决方案2】:

    查看我的代码:

       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();
    }
    }
    

    这个标志对于屏幕锁定时的触发活动很有用。

    【讨论】:

    • 在我的服务类中,我没有开始任何用于创建数据备份的活动。所以我认为这个标志不是必需的吗?
    • 所以把那个标志放在你的意图中。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多