Это код, в котором я настраиваю свой будильник и отправляю сигнал на приемник вещания.
AlarmManager alarmManager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
Log.e("TAG", "request code is " + requestCode + " in set alarm
method:");
Intent intent = new Intent(Alarm.this, AlertReceiver.class);
intent.putExtra("extra", "alarm on");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
assert alarmManager != null;
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
This is my Broadcast Receiver class
@Override
public void onReceive(Context context, Intent intent) {
Log.e("We are in the receiver", "Yay!");
String get_extra = intent.getExtras().getString("extra");
Log.e("What is the key? ", get_extra);
Intent service_intent = new Intent(context, RingtonePlayer.class);
service_intent.putExtra("extra", get_extra);
if (get_extra != null) {
context.startService(service_intent);
} else {
Log.e("TAG", "Sorry! cannot start the service");
}
}
Now from here my service class code:
MediaPlayer media_song;
int startId;
boolean isRunning;
database db;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
db = new database(this);
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// fetch the extra string from the alarm on/alarm off values
String state = intent.getExtras().getString("extra");
// fetch the whale choice integer values
Log.e("Ringtone extra is ", state);
assert state != null;
switch (state) {
case "alarm on":
startId = 1;
break;
case "alarm off":
startId = 0;
Log.e("Start ID is ", state);
break;
default:
startId = 0;
break;
}
// if else statements
// if there is no music playing, and the user pressed "alarm on"
// music should start playing
if (!this.isRunning && startId == 1) {
Log.e("there is no music, ", "and you want start");
media_song = MediaPlayer.create(this,
Settings.System.DEFAULT_ALARM_ALERT_URI);
media_song.start();
this.isRunning = true;
this.startId = 0;
}
// if there is music playing, and the user pressed "alarm off"
// music should stop playing
else if (this.isRunning && startId == 0) {
Log.e("there is music, ", "and you want end");
// stop the ringtone
media_song.stop();
media_song.reset();
this.isRunning = false;
this.startId = 0;
}
// can't think of anything else, just to catch the odd event
else {
Log.e("else ", "somehow you reached this");
}
return Service.START_STICKY;
}
private void toastmessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
// Tell the user we stopped.
Log.e("on Destroy called", "ta da");
this.isRunning = false;
}
}
Я пытаюсь активировать свой будильник. Мой будильник срабатывает отлично, когда пользователь находится в действии или действие остановлено. Когда приложение разрушилось, моя сигнализация не сработала. Любезно, кто-нибудь посоветует мне, как активировать сигнал тревоги, когда приложение или действие уничтожены или убиты.