如何在设备重启时启动警报?

时间:2016-10-07 23:24:16

标签: android alarmmanager alarm

我有这个警报,我正在尝试制作一个BroadcastReceiver,用于在设备重启时重新创建警报。问题是我不知道将什么放入BroadcastReceiver。

这是用于在重启设备时重新创建警报的BroadcastReceiver

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;


 public class AlarmSetter extends BroadcastReceiver {



@Override
public void onReceive(Context context, Intent intent) {
    //I don't know what to put here
    // Do your work related to alarm manager
}

}

这是其他类

AlarmActivity Class

 import android.app.Activity;
 import android.app.AlarmManager;
 import android.app.PendingIntent;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.view.Window;
 import android.view.WindowManager;
 import android.widget.TextView;
 import android.widget.TimePicker;
 import android.widget.ToggleButton;
 import java.util.Calendar;
 import java.util.concurrent.TimeUnit;

 public class AlarmActivity extends Activity {

AlarmManager alarmManager;
private PendingIntent pendingIntent;
private TimePicker alarmTimePicker;
private static AlarmActivity inst;
private TextView alarmTextView;




public static AlarmActivity instance() {
    return inst;
}

@Override
public void onStart() {
    super.onStart();
    inst = this;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm);

    alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
    alarmTextView = (TextView) findViewById(R.id.alarmText);
    ToggleButton alarmToggle = (ToggleButton)   findViewById(R.id.alarmToggle);
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}

public void onToggleClicked(View view) {
    if (((ToggleButton) view).isChecked()) {
        Log.d("MyActivity", "Alarm On");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
        calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        long time = calendar.getTimeInMillis(); 
        if(time < System.currentTimeMillis()) calendar.setTimeInMillis(time + TimeUnit.DAYS.toMillis(1));
        Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, myIntent, 0);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

    } else  {
        alarmManager.cancel(pendingIntent);
        setAlarmText("");
        Log.d("MyActivity", "Alarm Off");
    }
}

public void setAlarmText(String alarmText) {
    alarmTextView.setText(alarmText);
}
}

AlarmReceiver Class

  import android.app.Activity;
  import android.content.ComponentName;
  import android.content.Context;
  import android.content.Intent;
  import android.media.MediaPlayer;
  import android.media.RingtoneManager;
  import android.net.Uri;
  import android.support.v4.content.WakefulBroadcastReceiver;
  import android.view.WindowManager;


 public class AlarmReceiver extends WakefulBroadcastReceiver {



@Override
public void onReceive(final Context context, Intent intent) {
    //this will update the UI with message
    AlarmActivity inst = AlarmActivity.instance();
    inst.setAlarmText("Alarm! Wake up! Wake up!");

    //this will sound the alarm tone
    //this will sound the alarm once, if you wish to
    //raise alarm in loop continuously then use MediaPlayer and setLooping(true)
    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alarmUri == null) {
        alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }






    //this will send a notification message
    ComponentName comp = new ComponentName(context.getPackageName(),
            AlarmService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);


   intent.setClass( AlarmActivity.instance(), WelcomePage.class);



   AlarmActivity.instance().startActivity(intent);




}
}

AlarmService类

 import android.app.IntentService;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.support.v4.app.NotificationCompat;
 import android.util.Log;


public class AlarmService extends IntentService {

private NotificationManager alarmNotificationManager;

public AlarmService() {
    super("AlarmService");
}

@Override
public void onHandleIntent(Intent intent) {
    sendNotification("Wake Up! Wake Up!");
}

private void sendNotification(String msg) {
    Log.d("AlarmService", "Preparing to send notification...: " + msg);
    alarmNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, AlarmActivity.class), 0);

    NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
            this).setContentTitle("Alarm").setSmallIcon(R.drawable.icon)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);


    alamNotificationBuilder.setContentIntent(contentIntent);
    alarmNotificationManager.notify(1, alamNotificationBuilder.build());
    Log.d("AlarmService", "Notification sent.");
}
}

我已在AndroidManifest.xml中插入此权限

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

0 个答案:

没有答案