- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我没有找到解决这个荒谬问题的方法。 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 上测试了代码。
非常感谢!
最佳答案
我认为您必须使用 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 我管理接收到的 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/
我正在使用 AlarmManager 每天在通过时间选择器选择的特定时间触发通知。通知在同一天正确触发,但不是每天都正确重复!!! 这是使用setRepeating()设置通知的方法: public
我的程序旨在创建一个重复警报,触发一个广播接收器,进而发出通知。使用用户输入的时间间隔重复警报。 例如,如果我想将闹钟设置为每 10 秒运行一次,我该怎么做? am.setRepeating(Ala
我试图通过 AlarmManager 定期调用一个线程。问题是,在给定的时间段之后,OnReceive() 被调用了两次(为什么?),然后再也没有调用过。我在这里做错了什么? 我在 API 级别 23
我有像上午 9:00、下午 1:00、晚上 7:00 这样的时间,我设置了 3 个待处理的 Intent ,这些 Intent 每天使用 setRepeating() 方法递归触发。 (这里我将所有这
我在尝试设置每日通知时遇到问题。 我使用 AlarmManager 设置每天下午 6:00 的闹钟 Intent myIntent = new Intent(this, AlarmReceiver.c
这是我的代码: cal.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, hourOfDay, minute,Calendar.SEC
我已经实现了 AlarmManager 来每天唤醒手机一次以执行更新任务、更新小部件并在适用时发送通知。 我正在使用 setRepeating 和 ELAPSED_REALTIME_WAKEUP第一次
所以基本上我有这个代码,时间返回 24 小时时间并每天重复闹钟。 public setAlarm(String time, Context context){ String[] strTime
在我的 android 应用程序中,我设置了一个我想重复出现的闹钟,因此使用了 AlarmManager.setRepeating()。 我不想自己跟踪警报是否已设置(听起来是个坏主意,有时很容易失败
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
我有一个项目列表,我想几乎每天都发送通知。但不是在用户可能正在 sleep 的半夜。 可能是上午 8 点之后的任何时间。 我知道我需要通过 AlarmManager 使用它。 我非常熟悉 AlarmM
我没有找到解决这个荒谬问题的方法。 setRepeating() 和 setInexactRepeating() 根本不触发。我尝试了这两种方法。以下是我的代码: Intent intentServi
我正在创建一个应用程序,每天提醒用户 X 次某事。我想知道创建是否更好: 多个 setRepeating 警报,间隔为 24 小时 一个 setRepeating 闹钟,间隔为 1 小时 [或半小时]
要求:- 我需要在一周中选定的几天触发警报以及警报开始的日期(例如,我想在每周的周五和周六从 2017 年 5 月 26 日下午 6:45 开始触发警报) 问题:- 当从循环内调用 AlarmMana
我进入了我想在特定时间触发服务的场景。 据我所知...我需要使用 AlarmManager,我在听到这个问题,Using Alarmmanager to start a service at spec
在搜索了 stackoverflow 和网络之后,我仍然感到沮丧并遇到了问题。我正在尝试提供一项服务,该服务会定期轮询传感器并将该数据发送到某个地方。我从底层开始,只是试图让服务每隔 x 秒显示一次
这个问题在这里已经有了答案: to know next alarm time using setRepeating() method (1 个回答) 关闭 5 年前。 我根据我的应用程序要求使用 s
AlarmManager mgr= (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE); Intent i=new I
我的应用需要非常准确的重复闹钟计时。从 API 19 开始,AlarmManager setRepeating 现在不能准确地节省电量(保存树木和所有)。 是否有任何解决方法可以让 API 19 的
下面的参数是什么: alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
我是一名优秀的程序员,十分优秀!