重启Android Studio后如何重置警报

时间:2016-09-12 02:23:49

标签: java android android-studio alarmmanager repeatingalarm

我正在制作一个警报应用,根据用户选择的时间打开活动。一切都工作得很好,但重启会重置警报。我制作了一个ON BOOT RECEIVER,但我只想在用户想要它的情况下重启警报。我尝试使用.putExtra将数据发送到ON BOOT广播,但重启后数据会重置。我尝试在广播中使用SharedPreference设置条件但我仍然坚持如何使用它。 MainActivity

public class MainActivity extends AppCompatActivity {
NumberPicker hour;
NumberPicker min;
Button start;
Button stop;
TextView number;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    hour = (NumberPicker) findViewById(R.id.hour);
    min = (NumberPicker) findViewById(R.id.min);
    start = (Button) findViewById(R.id.starter);
    stop = (Button) findViewById(R.id.stopper);

    number = (TextView) findViewById(R.id.textView);

    hour.setMinValue(0);
    hour.setMaxValue(23);
    hour.setWrapSelectorWheel(false);

    min.setMinValue(1);
    min.setMaxValue(59);
    min.setWrapSelectorWheel(false);

    SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
    final int myIntValue = sp.getInt("your_int_key", -1);

    Intent myIntent = new Intent(MainActivity.this, BootCompletedReceiver.class);

    if (myIntValue == 1){

        myIntent.putExtra("alarm", 3);
        sendBroadcast(myIntent);

    }
    else if (myIntValue == 2){

       myIntent.putExtra("alarm", 4);
       sendBroadcast(myIntent);

    }
    else
    { stop.setBackgroundColor(Color.RED); }


    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, hour.getValue());
            calendar.set(Calendar.MINUTE, min.getValue());

            Intent intent = new Intent(getApplicationContext(),notification.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 120*1000, pendingIntent);


            SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("your_int_key", 1);
            editor.apply();

        }
    });

    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(),notification.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);

            SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("your_int_key", 2);
            editor.apply();

        }
    });

}}

广播

public class BootCompletedReceiver extends BroadcastReceiver {



@Override
public void onReceive(Context context, Intent intent) {

 if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {

        Intent i = new Intent(context, RestartAlarmsService.class);

        //ComponentName service = context.startService(i);
        context.startService(i);


    }
}}

服务

public class RestartAlarmsService extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {

    //code to restart Alarm

    return START_NOT_STICKY;
}

}

谢谢。

0 个答案:

没有答案