自从 API 21 开始,你可以使用
public AlarmManager.AlarmClockInfo getNextAlarmClock ()
http://developer.android.com/reference/android/app/AlarmManager.html#getNextAlarmClock()
现在,您正在尝试使用的代码是:
(PendingIntent.getBroadcast(MainActivity.this, 0,
new Intent(MainActivity.this, AlarmReceiver.class), PendingIntent.FLAG_NO_CREATE) != null);
基本上,您正在请求一个名为AlarmReceiver的先前和现有意图。但是他的AlarmReceiver指的是您自己的BroadcastReceiver。
正如您在之前发布的答案中看到的那样:
boolean alarmUp = (PendingIntent.getBroadcast(context, 0,
new Intent("com.my.package.MY_UNIQUE_ACTION"),
PendingIntent.FLAG_NO_CREATE) != null);
他们使用“MY_UNIQUE_ACTION”来查看意图是否存在。
此外,在这个网站上,您可以看到使用它的教程:
http://justcallmebrian.com/2010/04/27/using-alarmmanager-to-schedule-activities-on-android/
除非您能够访问android系统的AlarmReceiver并查看意图是否存在,否则您将无法请求“通用”的定时闹钟。这是您想要做的吗?如果是这种情况,您确定在没有任何预定闹钟的情况下,android系统的AlarmReceiver意图不会被系统创建吗?
似乎我们无法像我们想要的那样控制这些组件。
一个工作示例:
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blablabla.testa" >
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver
android:name=".MySuperReceiver"
android:label="MySuperReceiverName" />
</activity>
</application>
MySuperReceiver.java
package com.blablabla.testa;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.Calendar;
public class MySuperReceiver extends BroadcastReceiver {
public static final String TAG = MySuperReceiver.class.getSimpleName();
public static final String ACTION_ALARM_RECEIVER = "ACTION_ALARM_RECEIVER";
private Calendar c = Calendar.getInstance();
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null)
if (ACTION_ALARM_RECEIVER.equals(intent.getAction())) {
Log.d(TAG, new Exception().getStackTrace()[0].getMethodName() + " " + c.getTime());
}
}
}
而 MainActivity.java:
package com.blablabla.testa;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
public static final String TAG = "TEST APP:";
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetAlarmService();
CreateAlarm();
CheckForAlarm();
CancelAlarms();
CheckForAlarm();
}
private void GetAlarmService()
{
alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Log.d(TAG, " GET Alarm Service ! ");
}
private void CreateAlarm()
{
long aroundInterval = 1*60*1000;
Intent intent = new Intent(getApplicationContext(), MySuperReceiver.class);
intent.setAction(MySuperReceiver.ACTION_ALARM_RECEIVER);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), aroundInterval, pendingIntent);
Log.d(TAG, " CREATE Alarm ! ");
}
private void CancelAlarms()
{
Intent intent = new Intent(getApplicationContext(), MySuperReceiver.class);
intent.setAction(MySuperReceiver.ACTION_ALARM_RECEIVER);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
Log.d(TAG, " Cancel Alarm ! ");
}
private void CheckForAlarm()
{
Intent intent = new Intent(getApplicationContext() , MySuperReceiver.class);
intent.setAction(MySuperReceiver.ACTION_ALARM_RECEIVER);
boolean isWorking = (PendingIntent.getBroadcast(getApplicationContext() , 1001, intent, PendingIntent.FLAG_NO_CREATE) != null);
Log.d("TAG: TEST APP: ", "alarm is " + (isWorking ? "" : "not") + " working...");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
PendingIntent
是否存在的技术并不可靠。 - CommonsWareFLAG_NO_CREATE
的技巧并不可靠。 - CommonsWareAlarmManager
需要一个更好的 API。 - CommonsWare