我没有找到解决这个荒谬问题的方法。 setRepeating()和setInexactRepeating()根本不会触发。我试过这两种方法。以下是我的代码:
Intent intentService = new Intent(context, ServiceReceiver.class);
intentService.putExtra("checkStrikeAndNews", true);
PendingIntent pendingIntentSN = PendingIntent.getBroadcast(context, 0,intentService, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
//scheduling checkNews
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntentSN);
我也尝试过使用setInexactRepeating(),但它不起作用。我还使用了ELAPSED_REALTIME_WAKEUP和SystemClock.elapsedRealtime(),行为相同。
广播接收器ServiceReceiver.class完美地工作,它接收其他意图和警报意图(AlarmManager set()和setExact())没有问题。我在ICS,JellyBean和Marshmallow上测试了代码。
非常感谢!
答案 0 :(得分:8)
我认为您必须使用PendingIntent.getService()
代替PendingIntent.getBroadcast()
请确保您的服务已在Manifest.xml
最好使用setInexactRepeating()
电池续航时间
App.class 我在哪里设置闹钟
public class App extends Application {
private static final int INTERVAL = 1000 * 60;
private static final int DELAY = 5000;
@Override
public void onCreate() {
super.onCreate();
setupAlarm();
}
private void setupAlarm() {
PendingIntent myPendingIntent = PendingIntent.getService(this, 0, MyService.newIntent(this), 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, DELAY, INTERVAL, myPendingIntent);
}
}
MyService.class 我管理收到的意图
public class MyService extends IntentService {
private static final String TAG = MyService.class.getSimpleName();
private static final String EXTRA_CHECK = "checkStrikeAndNews";
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public MyService() {
super("MyService");
}
public static Intent newIntent(Context context) {
return new Intent(context, MyService.class) //
.putExtra(EXTRA_CHECK, true);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent.getBooleanExtra(EXTRA_CHECK, false)) {
Log.i(TAG, "I just received an intent");
}
}
}
Manifest.xml 我在哪里宣布我的服务
<manifest
package="com.darzul.test"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".App"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.MyService" />
</application>
</manifest>
编辑01/02/2015
以下是包含BroadcastReceiver
<强> App.class 强>
public class App extends Application {
private static final int INTERVAL = 1000 * 60;
private static final int DELAY = 5000;
@Override
public void onCreate() {
super.onCreate();
setupAlarm();
}
private void setupAlarm() {
Intent myIntent = new Intent(this, MyReceiver.class);
PendingIntent myPendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, DELAY, INTERVAL, myPendingIntent);
}
}
警告:如果您在SDK等于或大于5.1的设备上进行测试,则间隔警报不能少于1分钟。
自Android 5.1起,价值将被强制为60000;不要依赖于这一点 频繁的警报不利于电池寿命。从API 22开始,AlarmManager将覆盖近期和高频警报请求,将警报延迟至少5秒,并确保重复间隔至少为60秒。如果您确实需要在5秒钟之前完成工作,请发布延迟消息或运行至处理程序。
<强> MyReceiver.class 强>
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
}
}
<强>的Manifest.xml 强>
<manifest
package="com.darzul.test"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".App"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:exported="false"
android:name=".receiver.MyReceiver" />
</application>
</manifest>