0

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

警报管理器类:-

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

4

2 回答 2

1

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

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

于 2017-01-11T15:21:37.773 回答
0

问题是我使用的是pendingintent.getService()而不是Velislava正确指出的pendingIntent.getBroadcast()......

于 2017-01-11T15:42:16.870 回答