Trong hoạt động chính của tôi có nút trong đó. Trong onclick nghe im của nó chức năng gọi để thiết lập alarm.The báo động đang làm việc nhưng iam không thể để ngăn chặn nó .Can một số một giúp tôiCách dừng báo thức trong android
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setalarm();
}
});
}
private void setalarm() {
Calendar cal=Calendar.getInstance();
// cal.set(Calendar.MONTH,6);
// cal.set(Calendar.YEAR,2013);
// cal.set(Calendar.DAY_OF_MONTH,12);
cal.set(Calendar.HOUR_OF_DAY,18);
cal.set(Calendar.MINUTE,32);
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(this, "Alarm SET.", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Mote.java:
public class Mote extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Hello you have to take medicine I am Nitin Sharma";
long when = System.currentTimeMillis();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
final int NOTIF_ID = 1234;
NotificationManager notofManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, Alset.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText,when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
notofManager.notify(NOTIF_ID,notification);
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
Intent i = new Intent(context,Alset.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Alset.java:
public class Alset extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activitystop);
Log.e("IM here ","Im here");
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Alset.this, "Stop the alrm now", Toast.LENGTH_LONG).show();
}
});
}
}
Khi báo động bắt đầu tôi sẽ nhận được gọi lại trong máy thu của 01.234.. Từ đó tôi sẽ đến hoạt động Alset nơi tôi giữ nút dừng. Làm cách nào để dừng báo thức từ đây?
LƯU Ý: - Tôi đang mã hóa thời gian để đặt báo thức.
bạn cần phải hủy bỏ cảnh báo từ người quản lý báo động thử ==== AlarmManager alarmManager = (AlarmManager) _cntx.getSystemService (Context.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast (this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA); \t \t \t báo thứcManager.cancel (pendingIntent); – user1140237