您好我正在寻找有关如何在我的应用中关闭闹钟的一些问题,但没有任何帮助我。我有两个按钮打开和关闭,警报通常可以启动,但我无法将其关闭。
My AlarmActivity
public class AlarmActivity extends Activity {
AlarmManager alarmManager;
private PendingIntent pendingIntent;
private TimePicker alarmTimePicker;
private static AlarmActivity inst;
private TextView alarmTextView;
Button on, off;
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_my);
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
alarmTextView = (TextView) findViewById(R.id.alarmText);
Button on = (Button) findViewById(R.id.zapni);
Button off = (Button) findViewById(R.id.vypni);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
on.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
Log.d("MyActivity", "Alarm On");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
});
off.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent,0);
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
aManager.cancel(pIntent);
Log.d("MyActivity", "Alarm Off");
}
});
}
My AlarmReciever Class
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
AlarmActivity inst = AlarmActivity.instance();
inst.setAlarmText("Alarm! Wake up! Wake up!");
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
My AlarmService Class
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")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg)
.setSmallIcon(R.mipmap.ic_launcher);
alamNotificationBuilder.setContentIntent(contentIntent);
alarmNotificationManager.notify(1, alamNotificationBuilder.build());
Log.d("AlarmService", "Notification sent.");
}
}
我认为有些不对劲
off.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent,0);
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
aManager.cancel(pIntent);
Log.d("MyActivity", "Alarm Off");
}
});
答案 0 :(得分:0)
试试这段代码,
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
aManager.cancel(pendingIntent);
答案 1 :(得分:0)
PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(),1253, intent,0);
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
aManager.cancel(pIntent);
stopService(new Intent(AlarmActivity.this, AlarmReceiver.class));
你托盘就像它可能有帮助其他明智的参考this.