gpt4 book ai didi

Android:当闹钟响起时如何在闹钟接收器中获取requestCode

转载 作者:太空宇宙 更新时间:2023-11-03 12:33:36 25 4
gpt4 key购买 nike

用户可以设置多个闹钟。在这种情况下,我将警报数据存储到数据库中并创建一个具有唯一 requestCodePendingIntent。实际上,我使用数据库的行 ID 作为 requestCode,当警报触发时,我可以从数据库中获取特定警报的其他信息。

从我设置 Intent 的地方:

private void setInstantAlarm(Calendar timeFromNow, int pos){
try{
Intent intent = new Intent(context, AlarmReceiverActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(context, pos, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeFromNow.getTimeInMillis(), pendingIntent);

}catch (NumberFormatException e){
Toast.makeText(context, "Something went wrong!", Toast.LENGTH_SHORT).show();
}
}

在我的警报接收器类中,我想要我设置为 requestCode 的行 ID:

public class AlarmReceiverActivity extends Activity {
private MediaPlayer mMediaPlayer;
private PowerManager.WakeLock mWakeLock;

@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Wake Log");
mWakeLock.acquire();

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,

WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

setContentView(R.layout.alarm);

Button stopAlarm = (Button) findViewById(R.id.btnStopAlarm);
stopAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMediaPlayer.stop();
finish();
}
});
playSound(this, getAlarmUri());
}

private void playSound(Context context, Uri alert){
mMediaPlayer = new MediaPlayer();
try{
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0){
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
}catch (IOException e){
Log.i("AlarmReceiver", "No audio files are Found!");
}
}

private Uri getAlarmUri(){
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null){
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alert == null){
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
return alert;
}

protected void onStop(){
super.onStop();
mWakeLock.release();
}
}

那么我如何才能获得未决 Intent 的 requestCode 或此 AlarmReceiverActivity 类中的行 ID?

最佳答案

在创建 Intent 时,将 Extra 添加到其中:

Intent intent = new Intent(context, AlarmReceiverActivity.class);
intent.putExtra("requestCode",pos);
PendingIntent pendingIntent =
PendingIntent.getActivity(context, pos, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeFromNow.getTimeInMillis(), pendingIntent);

在接收器中:

@Override
public void onReceive(Context pContext, Intent pIntent){
// retrieve the value
int code= pIntent.getIntExtra("requestCode", 1);

}

关于Android:当闹钟响起时如何在闹钟接收器中获取requestCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26075893/

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