重启后未正确设置alarmManager

时间:2015-10-27 17:09:27

标签: java android alarmmanager

我希望每天(13:00)通过alarmManager收到通知,但是当设备重新启动时,警报仅激活一天,并且在第2天或第3天不起作用,除非我运行我的应用程序。

为什么它只运行一次?

MainActivity.class:

public class MainActivity extends ActionBarActivity implements OnClickListener 
{
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   alarmKurulum();
}



public void alarmKurulum() {
    Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    Calendar alarmStartTime = Calendar.getInstance();

    alarmStartTime.set(Calendar.HOUR_OF_DAY, 18);
    alarmStartTime.set(Calendar.MINUTE, 05);
    alarmStartTime.set(Calendar.SECOND, 00);
    alarmStartTime.set(Calendar.MILLISECOND, 0);

    Calendar now = Calendar.getInstance();

    if(alarmStartTime.getTimeInMillis() < now.getTimeInMillis()) {
        alarmStartTime.setTimeInMillis(alarmStartTime.getTimeInMillis() + 86400000);
    }

    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), pendingIntent);
}

AlarmReceiver.class:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service1 = new Intent(context, AlarmService.class);
        context.startService(service1); 
    }
}

TurnonAlarm.class(用于重启设备)

public class TurnOnAlarmReceive extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Intent alarmIntent = new Intent(context, AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

            Calendar alarmStartTime = Calendar.getInstance();

            alarmStartTime.set(Calendar.HOUR_OF_DAY, 18);
            alarmStartTime.set(Calendar.MINUTE, 05);
            alarmStartTime.set(Calendar.SECOND, 00);
            alarmStartTime.set(Calendar.MILLISECOND, 0);


            Calendar now = Calendar.getInstance();

            if(alarmStartTime.getTimeInMillis() < now.getTimeInMillis()) {
                alarmStartTime.setTimeInMillis(alarmStartTime.getTimeInMillis() + 86400000);
            }
            alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), pendingIntent);         
        }
    }
}

的AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ferhengakurdi9865"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="23" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light" >


        <service android:name=".AlarmService" />                
        <receiver android:name=".AlarmReceiver" />
        <receiver android:name=".TurnOnAlarmReceive"  android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

0 个答案:

没有答案