嗨,我一直在尝试向我的应用添加警报。
但是我设置了闹钟之后,它并没有停止,我会永远演奏。
我使用的Java类是警报接收器,activity-alarm.java和xml。
以及如何从通知中停止警报。预先感谢。
这是主要活动。
`公共类MainActivity扩展了AppCompatActivity {
AlarmManager alarmManager;
private PendingIntent pending_intent;
private TimePicker alarmTimePicker;
private TextView alarmTextView;
private AlarmReceiver alarm;
MainActivity inst;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
//alarm = new AlarmReceiver();
alarmTextView = (TextView) findViewById(R.id.alarmText);
final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
// Get the alarm manager service
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// set the alarm to the time that you picked
final Calendar calendar = Calendar.getInstance();
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
Button start_alarm= (Button) findViewById(R.id.start_alarm);
start_alarm.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
calendar.add(Calendar.SECOND, 3);
//setAlarmText("You clicked a button");
final int hour = alarmTimePicker.getCurrentHour();
final int minute = alarmTimePicker.getCurrentMinute();;
Log.e("MyActivity", "In the receiver with " + hour + " and " +
minute);
setAlarmText("You clicked a " + hour + " and " + minute);
calendar.set(Calendar.HOUR_OF_DAY,
alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,
alarmTimePicker.getCurrentMinute());
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(MainActivity.this,
0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pending_intent);
// now you should change the set Alarm text so it says
something nice
setAlarmText("Alarm set to " + hour + ":" + minute);
//Toast.makeText(getApplicationContext(), "You set the alarm",
Toast.LENGTH_SHORT).show();
}
});
Button stop_alarm= (Button) findViewById(R.id.stop_alarm);
stop_alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int min = 1;
int max = 9;
Random r = new Random();
int random_number = r.nextInt(max - min + 1) + min;
Log.e("random number is ", String.valueOf(random_number));
myIntent.putExtra("extra", "no");
sendBroadcast(myIntent);
alarmManager.cancel(pending_intent);
setAlarmText("Alarm canceled");
//setAlarmText("You clicked a " + " canceled");
}
});
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
}`
答案 0 :(得分:0)
我找到了解决方案。
添加此警报后停止:
x[i+1]
工作k
Intent intent = new Intent(this, RingtonePlayingService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);