从棉花糖及更高版本中的服务启动时,AlarmManager失败

时间:2019-02-04 18:43:07

标签: android alarmmanager

正在尝试在服务中设置简单的警报管理器。

test_cases。

如果我从“活动”中启动警报管理器,它将起作用

    Intent intentzz = new Intent(getApplicationContext(), Homev2.class);
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intentzz, PendingIntent.FLAG_CANCEL_CURRENT);

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 5);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);

但是,如果我尝试从服务中启动它,那只是没有,而且我也没有收到任何错误

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
   // throw new UnsupportedOperationException("Not yet implemented");
    return null;
}

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e("we here", "here");

    Intent intent1 = new Intent(getApplicationContext(), SettingsActivity.class);
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1,intent1, 0);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, 5);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
    return super.onStartCommand(intent, flags, startId);
}
}

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

尝试更改此内容:

['barcelona', 'bogota', 'chennai', 'hyderabad', 'istanbul']

对此:

    Intent intent1 = new Intent(getApplicationContext(), SettingsActivity.class);
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1,intent1, 0);