gpt4 book ai didi

java - AlarmManager 无法通过 bootservice 接收器工作

转载 作者:行者123 更新时间:2023-12-01 09:01:58 36 4
gpt4 key购买 nike

我已经制作了一个需要触发警报的应用程序。当从某些 Activity 安排警报时,警报管理器可以正常工作。由于重新启动时警报会被删除,因此我制作了一个 boot_Receiver 来重新安排警报。但问题是,当从 bootreceiver 类调度警报时,警报不会被触发。请帮忙..

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

}}

list 文件:-

<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 类中添加了振动器,检查它是否在启动时被调用。手机振动..这意味着该类在启动时被调用,但闹钟部分不起作用

最佳答案

我认为对于 HTC 您还应该添加 IntentFilters
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>

另外,我认为应该是
PendingIntent.getBroadcast(context, 2100, x, 0);
而不是
PendingIntent.getService(context,2100,x,0);

关于java - AlarmManager 无法通过 bootservice 接收器工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41594180/

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