Android setRepeating() 和 setInexactRepeating() 不触发
Android setRepeating() and setInexactRepeating() do not fire
我没有找到解决这个荒谬问题的办法。 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 上测试了代码。
非常感谢!
我认为你必须使用 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 分钟。
Value will be forced up to 60000 as of Android 5.1; don't rely on this to be exact
Frequent alarms are bad for battery life. As of API 22, the AlarmManager will override near-future and high-frequency alarm requests, delaying the alarm at least 5 seconds into the future and ensuring that the repeat interval is at least 60 seconds. If you really need to do work sooner than 5 seconds, post a delayed message or runnable to a Handler.
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>
我没有找到解决这个荒谬问题的办法。 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 上测试了代码。
非常感谢!
我认为你必须使用
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 分钟。
Value will be forced up to 60000 as of Android 5.1; don't rely on this to be exact Frequent alarms are bad for battery life. As of API 22, the AlarmManager will override near-future and high-frequency alarm requests, delaying the alarm at least 5 seconds into the future and ensuring that the repeat interval is at least 60 seconds. If you really need to do work sooner than 5 seconds, post a delayed message or runnable to a Handler.
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>