我已經制作了一個需要觸發報警的應用程序.Alarm Manager在從某些活動計劃時工作正常。由於警報在重新啓動時被刪除,因此我使用boot_Receiver重新安排警報。但問題在於,警報在從bootreceiver類安排時不會觸發。請幫助..AlarmManager不能從開機服務接收器工作
AlarmManager類: -
public class NotifyService extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Notification_Holder notifholder;
Type t=new TypeToken<Notification_Holder>(){}.getType();
Gson js=new Gson();
notifholder=js.fromJson(intent.getStringExtra("one"),t);
Notification_Creator notifcreator=new Notification_Creator(notifholder.title,notifholder.content,notifholder.cal,context);
notifcreator.create_notification();
}
}
BootReceiver類: -
public class AtBoot extends BroadcastReceiver {
SharedPreferences sharedPreferences;
AlarmManager alarmManager;
Intent x;
PendingIntent pendingIntent;
@Override
public void onReceive(Context context, Intent intent) {
sharedPreferences = context.getSharedPreferences("todoshared", Context.MODE_PRIVATE);
String f = sharedPreferences.getString("todolist", null);
Gson json = new Gson();
alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
vClass.notes = Notification_Holder.convert_from_jason(f);
x=new Intent(context,NotifyService.class);
pendingIntent=PendingIntent.getService(context,2100,x,0);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),pendingIntent);
Vibrator vib=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
if(vib.hasVibrator()) {
vib.vibrate(1500);
}
}}
清單文件: -
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:icon="@mipmap/ic_launcher"
android:theme="@style/MyAppBar">
<activity
android:name=".scrapper"
android:noHistory="true"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".schedule"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".workSpace"
android:theme="@style/AppTheme.NoActionBar" />
<receiver android:name=".widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
<service
android:name=".widgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<receiver android:name=".NotifyService" />
<activity
android:name=".showSubject"
android:theme="@style/AppTheme.popupTheme" />
<activity android:name=".splashScreen"
android:theme="@style/AppTheme.NoActionBar"
android:noHistory="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AtBoot"
android:enabled="true"
android:exported="true"
android:label="AtBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
我在AtBoot類中添加了振動器,檢查它是否在啓動時被調用。並且手機vibrates..This意味着該類被調用在啓動時,但鬧鐘部分不工作