gpt4 book ai didi

Android setRepeating() 和 setInexactRepeating() 不触发

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:03:01 25 4
gpt4 key购买 nike

我没有找到解决这个荒谬问题的方法。 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 工作完美,它可以毫无问题地接收其他 Intent 和警报 Intent (AlarmManager set() 和 setExact())。我在 ICS、JellyBean 和 Marshmallow 上测试了代码。

非常感谢!

最佳答案

  1. 我认为您必须使用 PendingIntent.getService() 而不是 PendingIntent.getBroadcast()

  2. 确保您的服务已在您的 Manifest.xml

  3. 中声明
  4. 电池生命周期最好使用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 我管理接收到的 Intent

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

的代码

应用类

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

list .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>

关于Android setRepeating() 和 setInexactRepeating() 不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33053651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com