我尝试在8:30使用警报管理器发送意图,但警报管理员从未在8:30左右向接收者发送意图。
我在setAlarm()方法中搞砸了一些东西,但我无法理解。 此外,许多帖子还提到清单文件中的服务/接收器可能被声明错误,但我不确定如何在清单中正确包含接收器和服务。我试图合并包名称,但它仍然不起作用。
public class EventAlarmReceiver extends WakefulBroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
private PendingIntent alarmIntent1;
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, EventSchedulingService.class);
startWakefulService(context, service);
}
public void setAlarm(Context context) {
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, EventSchedulingService.class);
intent.putExtra("silent", true);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// Set the alarm's trigger time to 8:30 a.m.
calendar.set(Calendar.HOUR_OF_DAY,8);
calendar.set(Calendar.MINUTE, 30);
System.out.println(calendar.getTimeInMillis());
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}
}
这是我的服务文件
package edu.ssui.smartsilent;
public class EventSchedulingService extends IntentService {
public EventSchedulingService() {
super("EventSchedulingService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("SService", "m1");
AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if(intent.getExtras().getBoolean("silent")==true){
mgr.setStreamVolume(AudioManager.STREAM_RING, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_SYSTEM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
}
else
{
mgr.setStreamVolume(AudioManager.STREAM_RING, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_RING)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_ALARM, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_ALARM)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_SYSTEM, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_SYSTEM)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_NOTIFICATION, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
}
Log.d("SService", "m1");
EventAlarmReceiver.completeWakefulIntent(intent);
}
}
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.ssui.smartsilent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".EventAlarmReceiver"></receiver>
<service android:name=".EventSchedulingService" />
</application>
</manifest>
答案 0 :(得分:1)
您正在致电:
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
PendingIntent.getBroadcast(context,intent)将检索一个pendingintent,它将使用您传递的上下文和意图执行广播。因此,例如,它将调用Context.sendBroadcast(intent),它将意图发送给所有感兴趣的BroadcastReceivers。
而不是:
Intent intent = new Intent(context, EventSchedulingService.class);
尝试将BroadcastReceiver传递给intent构造函数。
答案 1 :(得分:0)
试试这个:
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, EventAlarmReceiver.class);
intent.putExtra("silent", true);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
// calendar.setTimeInMillis(System.currentTimeMillis());
// Set the alarm's trigger time to 8:30 a.m.
calendar.set(Calendar.HOUR_OF_DAY,8);
calendar.set(Calendar.MINUTE, 30);
System.out.println(calendar.getTimeInMillis());
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
还要输入日志以进行调试,并确保调用setAlarm()。
答案 2 :(得分:0)
将此权限添加到您的清单:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>