我坚持在Android应用程序中停止(取消)闹钟。我会在这里粘贴我的代码,请帮帮我。
我的MainActivity.Java类:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText time_et;
Button start_alarm_btn, stop_alarm_btn;
Intent intent;
PendingIntent pendingIntent;
AlarmManager alarmManager;
AlaramReceiver alarmReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
time_et = (EditText) findViewById(R.id.set_time_et);
start_alarm_btn = (Button) findViewById(R.id.start_alarm);
stop_alarm_btn = (Button) findViewById(R.id.stop_alarm);
start_alarm_btn.setOnClickListener(this);
stop_alarm_btn.setOnClickListener(this);
alarmReceiver= new AlaramReceiver();
}
public void setAlarm(View view) {
if (!time_et.getText().toString().equals("")) {
int i = Integer.parseInt(time_et.getText().toString());
intent = new Intent(this, AlaramReceiver.class);
pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 280192, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm will set in " + i + " seconds",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this,"Please Provide time ",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onStop() {
super.onStop();
if (alarmManager !=null){
pendingIntent.cancel();
alarmManager.cancel(pendingIntent);
Toast.makeText(this, "alarm disabled!!!", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View view) {
if(view.getId() == R.id.start_alarm){
setAlarm(view);
}
if (view.getId() == R.id.stop_alarm){
pendingIntent.cancel();
alarmManager.cancel(pendingIntent);
Toast.makeText(this, "Alarm is disabled", Toast.LENGTH_SHORT).show();
}
}
}
我的AlarmReceiver.java类:
public class AlaramReceiver extends BroadcastReceiver {
MediaPlayer mp;
Ringtone ringtoneSound;
public AlaramReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
ringtoneSound = RingtoneManager.getRingtone(context.getApplicationContext(), ringtoneUri);
if (ringtoneSound != null) {
ringtoneSound.play();
}
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
我的Content_main.xml文件:
public class AlaramReceiver extends BroadcastReceiver {
MediaPlayer mp;
Ringtone ringtoneSound;
public AlaramReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
ringtoneSound = RingtoneManager.getRingtone(context.getApplicationContext(), ringtoneUri);
if (ringtoneSound != null) {
ringtoneSound.play();
}
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
我不知道我在哪里犯这个错误。请帮帮我......提前致谢。