计划的警报管理器无法正常工作 Android
Posted
技术标签:
【中文标题】计划的警报管理器无法正常工作 Android【英文标题】:Scheduled Alarm Manager not working Android 【发布时间】:2015-05-10 11:06:41 【问题描述】:我正在尝试启动每天在特定时间重复的警报服务。关于这个,我已经经历了很多关于堆栈溢出的线程,但没有运气。我遵循了一些教程:http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/ 和 http://javatechig.com/android/repeat-alarm-example-in-android
我的服务从未启动,我不明白为什么。以下是我的代码:
我的清单文件:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application>
<service android:name="com.paper.DownloadService" android:enabled="true"/>
<receiver android:name="com.paper.MyReceiver" ></receiver>
</application>
我的接收类:
public class MyReceiver extends BroadcastReceiver
@Override
public void onReceive(Context rcontext, Intent intent)
Log.e("Main Activity", "inside on receive of myreceiver");
Intent service1 = new Intent(rcontext, DownloadService.class);
rcontext.startService(service1);
我的服务等级:
public class DownloadService extends Service
@Override
public void onCreate()
// TODO Auto-generated method stub
Log.e("Download Service", "CREATED");
@SuppressLint( "SimpleDateFormat", "NewApi" ) @Override
public int onStartCommand(Intent intent, int flags, int startId)
// TODO Auto-generated method stub
Log.e("Download Service", "STARTED");
return START_NOT_STICKY;
我的主要活动(在创建方法内部):
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 29);
Intent myIntent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
在这里,我尝试在每天下午 3:29 设置闹钟,但该服务在那个时间或任何时间都没有启动。任何帮助,将不胜感激。谢谢!
【问题讨论】:
这是Log.e("Main Activity", "inside on receive of myreceiver");
显示在Log
中吗?还有calendar.getTimeInMillis(),
和AlarmManager.INTERVAL_DAY
的值是多少?
你是否设置了这个权限这是我为使其正常工作所做的:
1) 在我的清单文件中添加了<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
。
2) 将我的主要活动中的代码更改为:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 3);
calendar.set(Calendar.MINUTE, 29);
calendar.set(Calendar.AM_PM, Calendar.PM);
Intent myIntent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
希望有人觉得这有帮助!
【讨论】:
com.android.alarm.permission.SET_ALARM 仅当您需要使用 setTime() 设置系统挂钟时间时才需要。 developer.android.com/reference/android/app/… @Shreya。即使添加了该权限,它在我的情况下也不起作用。事实上,在 Android 4.4 及更低版本上,它根本不会触发。但在 Android > 4.4 上,它确实会触发,但不会在预定时间触发【参考方案2】:主要活动
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (60 * 1000), pendingIntent); // 60 seconds after the current time
报警接收器
public class MyBroadcastReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
Log.e("Testing","ok");
清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true" />
</application>
</manifest>
对于后台工作,将其添加到主要活动中(如果应用程序删除到最近)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName))
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
【讨论】:
以上是关于计划的警报管理器无法正常工作 Android的主要内容,如果未能解决你的问题,请参考以下文章
警报管理器不工作... android.app.ReceiverRestrictedContext
Android 6.0 处于打盹模式时如何让闹钟管理器工作?