启动后Android警报管理器

时间:2015-05-21 09:07:58

标签: android alarmmanager boot

我的闹钟代码工作正常。但是在开机后我无法重新安排我的闹钟。你能告诉我我错在哪里吗?

正如您在DeviceBootReciever中看到的,我放了吐司。但即便是这种吐司也不会出现。这意味着我无法听完启动。

但我不明白其中的原因。我在这里提到了我的代码。任何帮助将不胜感激!

MainActivity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            start();
        }
    });

    findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cancel();
        }
    });

    findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startAt10();
        }
    });

}

public void start() {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int interval = 8000; // tekrar süresi milisaniye cinsinden

    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
    Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}

public void cancel() {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.cancel(pendingIntent);
    Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}

public void startAt10() {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int interval = 8000; // tekrar süresi milisaniye cinsinden

    /* Set the alarm to start at 10:30 AM */
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    int hour = 14;
    int min= 43;
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, min);

    /* Repeating on every 20 minutes interval */
    manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            interval, pendingIntent);
    Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}

AlarmReciever.class:

public class AlarmReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent){
    Toast.makeText(context,"AlarmReceiver Calisiyor",Toast.LENGTH_SHORT).show();
}

}

DeviceBootreciever:

public class DeviceBootReceiver extends BroadcastReceiver {

public void  onReceive(Context context,Intent intent){
    if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
        /* Setting the alarm here */





        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        int interval = 8000;
        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);

        Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show();
    }
}

}

清单:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>


    <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>
    <!-- Will not be called unless the application explicitly enables it -->
    <receiver android:name=".AlarmReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

    <receiver android:name=".DeviceBootReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>

</application>

0 个答案:

没有答案