gpt4 book ai didi

android - 从具有相同 pendingintent 的另一个 Activity 中取消警报

转载 作者:行者123 更新时间:2023-11-29 20:02:28 25 4
gpt4 key购买 nike

我有一个问题,整天都在搜索,但无法正确解决

如何取消另一个 Activity (alarm_Time Activity )的警报?我认为我的问题在于我曾经取消与设置闹钟相同的 pendingintent

这里是 alarm_Time Activity 中取消闹钟的代码:

    final Intent intent = getIntent();
int alarm_No = intent.getIntExtra("alarm_No",0);
pending_intent = PendingIntent.getBroadcast(alarm_Time.this,alarm_No, intent, PendingIntent.FLAG_UPDATE_CURRENT);


//intialize alarm_off button
Button alarm_off = (Button) findViewById(R.id.alarm_off);
// create on click listner to end alarm
//final Intent finalIntent = intent;
alarm_off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//method that change update_txt txtbox
// set_alarm_text("Alarm off!");
//cancel the alarm
alarmManager.cancel(pending_intent);
// put extra string into my_intent
// tells the clock that you pressed the "alarm off" button
intent.putExtra("extra", "alarm off");
// stop the ringtone
sendBroadcast(intent);
}
});

设置闹钟的 Activity 代码(MainActivity2):

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); ///
this.context = this ;

update_txt = (TextView) findViewById(R.id.update_txt);
info_txt = (TextView) findViewById(R.id.info_txt);

final int[] h = {21, 21};
final int[] m = {15, 16};
final int length = h.length;

final Intent[] intent = new Intent[length];
final Calendar[] cal = new Calendar[length];
final AlarmManager[] alarmManager = new AlarmManager[length];
final PendingIntent[] pending_intent = new PendingIntent[length]; ;

for(int i =0; i<length; i++) {
cal[i] = Calendar.getInstance();
cal[i].set(Calendar.HOUR_OF_DAY, h[i]);
cal[i].set(Calendar.MINUTE, m[i]);
if (cal[i].getTimeInMillis() < System.currentTimeMillis())
{ cal[i].add(Calendar.DAY_OF_MONTH, 1); }

intent[i] = new Intent(this.context, AlarmReceiver.class);
alarmManager[i] = (AlarmManager) getSystemService(ALARM_SERVICE);

}



Button alarm_on = (Button) findViewById(R.id.alarm_on);
alarm_on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String h1_string = String.valueOf(h[0]);
String m1_string = String.valueOf(m[0]);


for(int i =0; i<length; i++) {


set_alarm_text("Alarm on!" + h1_string + ":" + m1_string);
intent[i].putExtra("extra", "alarm on");
intent[i].putExtra("Alarm_no", i);
pending_intent[i] = PendingIntent.getBroadcast(MainActivity2.this, i, intent[i], PendingIntent.FLAG_UPDATE_CURRENT );
alarmManager[i].set(AlarmManager.RTC_WAKEUP, cal[i].getTimeInMillis(), pending_intent[i]);

}


}
});

//intialize alarm_off button
Button alarm_off = (Button) findViewById(R.id.alarm_off);
// create on click listner to end alarm
alarm_off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle extras = getIntent().getExtras();
int alarm_No = -1;
if (extras != null) {
alarm_No = extras.getInt("alarm_No2");
}

Log.v("help", String.valueOf(alarm_No));

set_alarm_text("Alarm off!");
alarmManager[0].cancel(pending_intent[0]);
intent[0].putExtra("extra", "alarm off");
sendBroadcast(intent[0]);
}
});
}

private void set_alarm_text(String output) {
update_txt.setText(output);
}

private void set_info_text(String output) {
info_txt.setText(output);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver{


@Override
public void onReceive(Context context, Intent intent) {
Log.e("We are in the receiver", "Yay");

//fetch extra strings from the intent >>> get_ur_string =extra // alarm on - alarm off
String get_ur_string = intent.getExtras().getString("extra");
int No = intent.getExtras().getInt("Alarm_no");


// Log.e("What is the key? ", get_ur_string);


//create an intent to the ringtone service
Intent service_intent = new Intent(context, RingtonePlayingService.class);

// pass the extra from Main Activity to Ringtone Playing Service
service_intent.putExtra("extra", get_ur_string);
service_intent.putExtra("Alarm_no", No);

//start the ringtone services
context.startService(service_intent);



}

}

铃声播放服务.java

public class RingtonePlayingService extends Service {

MediaPlayer media_song;
int startId;
boolean isRunning;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i("LocalService", "Received start id " + startId +": " + intent);

// fetch the extra string values
String state = intent.getExtras().getString("extra");
int alarm_No = intent.getExtras().getInt("Alarm_no");

// Log.e("Ringtone state:extra", state);


// notification
// set up the notification service
NotificationManager notify_manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// set up an intent that goes to the Main Activity
Intent intent_main_activity = new Intent(this.getApplicationContext(), MainActivity2.class);
intent.putExtra("extra", "alarm on");
intent.putExtra("Alarm_no2", alarm_No);
// set up a pending intent
PendingIntent pending_intent_main_activity = PendingIntent.getActivity(this, alarm_No,
intent_main_activity, 0);

// make the notification parameters
Notification notification_popup = new Notification.Builder(this)
.setContentTitle("An alarm is going off!")
.setContentText("Click me!")
.setSmallIcon(R.drawable.image)

.setContentIntent(pending_intent_main_activity)
.setAutoCancel(true)
.build();

// set up notification atart command
notify_manager.notify(0, notification_popup);




// this converts the extra strings from the intent to start IDs, values 0 or 1
assert state != null;
switch (state) {
case "alarm on":
startId = 1;
int alarm_num = intent.getExtras().getInt("No");
break;
case "alarm off":
startId = 0;
Log.e("Start ID is", state);
break;
default:
startId = 0;
break;
}

// if else statments

// if there is no music playing, and the user pressing "alarm on"
// music should start playing
if( !this.isRunning && startId == 1 ){
Log.e("there is no music","and u want start");
// create an instant of the media player
media_song = MediaPlayer.create(this, R.raw.tone);
// start the ringtone
media_song.start();

this.isRunning = true;
this.startId = 0;

}
// if there is music playing, and the user pressing "alarm off"
// music should stop playing
else if( this.isRunning && startId == 0 ){
Log.e("there is music", "and u want stop");

// stop the ringtone
media_song.stop();
media_song.reset();

this.isRunning = false;
this.startId = 0;

}





return START_NOT_STICKY;
}

@Override
public void onDestroy() {
//tell the user we stopped
Log.e("on Destroy called", "ta da");

super.onDestroy();
this.isRunning = false;

}

}

最佳答案

您正在尝试通过这种方式从 Activity 中取消警报:

final Intent intent = getIntent();
int alarm_No = intent.getIntExtra("alarm_No",0);
pending_intent = PendingIntent.getBroadcast(alarm_Time.this,alarm_No, intent, PendingIntent.FLAG_UPDATE_CURRENT);
...
//cancel the alarm
alarmManager.cancel(pending_intent);

但是你这样设置闹钟:

intent[i] = new Intent(this.context, AlarmReceiver.class);
intent[i].putExtra("Alarm_no", i);
pending_intent[i] = PendingIntent.getBroadcast(MainActivity2.this, i, intent[i], PendingIntent.FLAG_UPDATE_CURRENT );
alarmManager[i].set(AlarmManager.RTC_WAKEUP, cal[i].getTimeInMillis(), pending_intent[i]);

您使用了错误的 Intent。在您的 Activity 中,当您调用 getIntent() 时,它将返回用于启动 ActivityIntent .您可以在调用 PendingIntent.getBroadcast() 时使用它。这不会返回正确的 PendingIntent。您需要将设置闹钟时使用的相同 Intent 传递给对 PendingIntent.getBroadcast() 的调用。这应该看起来像这样:

final Intent intent = new Intent(this, AlarmReceiver.class);
int alarm_No = intent.getIntExtra("alarm_No",0);
pending_intent = PendingIntent.getBroadcast(alarm_Time.this,alarm_No, intent, PendingIntent.FLAG_UPDATE_CURRENT);
...
//cancel the alarm
alarmManager.cancel(pending_intent);

此外,我注意到您在额外的地方传递了警报号码,如下所示:

intent[i].putExtra("Alarm_no", i);

使用键"Alarm_no",但您尝试使用alarmtime_Activity获取警报编号

int alarm_No = intent.getIntExtra("alarm_No",0);

使用键 "alarm_No"。这些键只是 String,因此区分大小写。您可能没有使用完全相同的 key 吗?我无法判断,因为您还没有发布用于启动 alarmtime_Activity 的代码。

关于android - 从具有相同 pendingintent 的另一个 Activity 中取消警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36084830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com