我有一个可以创建多个闹钟的应用。一切正常,但是,对于其他一些功能(如更新日志)我需要在触发警报时检索alarmID。
以下是设置警报的代码:
public void setAlarm(){
Intent intentAlarm = new Intent(c,AlarmReceiver.class);
intentAlarm.putExtra("userid", userid);
// create the object
AlarmManager alarmManager = (AlarmManager)
c.getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent;
alarmIntent = PendingIntent.getActivity(c, alarmID, intentAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,timeToMiliseconds(hour,minute),
AlarmManager.INTERVAL_DAY,alarmIntent);
Toast.makeText(c,"Alarm set for " + toTime(hour,minute),Toast.LENGTH_SHORT).show();
Log.d("setAlarm()",alarm.toString());
}
警报接收器是一项活动(工作正常)。我只需要检索当前被触发的警报ID。
public class AlarmReceiver extends Activity {
private MediaPlayer mMediaPlayer;
private String scanContent;
private static final int ZBAR_SCANNER_REQUEST = 0;
String userid;
Button backBtn;
private PowerManager.WakeLock wakelock;
private ListView medBoxList;
ArrayList<HashMap<String, String>> medList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
ActionBar actionBar = getActionBar();
actionBar.hide();
/**
* Wakeup device if it's sleeping
*/
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wakelock.acquire();
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Intent alarmActivityIntent = getIntent();
userid = alarmActivityIntent.getStringExtra("userid");
playSound(this, getAlarmUri());
Toast.makeText(this, "Take Medication", Toast.LENGTH_LONG).show();
Button stopAlarm = (Button) findViewById(R.id.scanQR);
Button skipBtn = (Button) findViewById(R.id.skipBtn);
backBtn = (Button)findViewById(R.id.backBtn);
//Click button to scan and update userlog
//Click button to scan and update userlog
stopAlarm.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
mMediaPlayer.stop();
launchQRScanner();
}
});
//Click button to skip
skipBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
mMediaPlayer.stop();
}
});
}
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) {
System.out.println("OOPS");
}
}
//Get an alarm sound. Try for an alarm. If none set, try notification,
//Otherwise, ringtone.
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;
}
public void launchQRScanner() {
mMediaPlayer.stop();
Intent intent = new Intent(this, ZBarScannerActivity.class);
intent.putExtra(ZBarConstants.SCAN_MODES, new int[]{Symbol.QRCODE});
startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mMediaPlayer.stop();
scanContent = data.getStringExtra(ZBarConstants.SCAN_RESULT);
finish();
if(scanContent != null){
updateUserLog(scanContent);
Intent nextScreen = new Intent(getApplicationContext(),UserlogListActivity.class);
startActivity(nextScreen);
}
}
//Disable alarm with button instead of scan
public void alarmStopper(View v){
mMediaPlayer.stop();
Intent nextScreen = new Intent(getApplicationContext(),UserlogListActivity.class);
startActivity(nextScreen);
}
//TAKE MED ACTIVITY (MOVE TO )
public void updateUserLog(String scanContent){
//Toast.makeText(this, "Adding Userlog = " + scanContent, Toast.LENGTH_SHORT).show();
if(scanContent != null){
String userid,medname,tabstaken,dob;
//Break the scan content into strings for each variable
StringTokenizer st = new StringTokenizer(scanContent, ",");
dob = st.nextToken();
medname = st.nextToken();
tabstaken = st.nextToken();
if(dob != "" && medname != "" && tabstaken != ""){
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
HashMap<String,String> userLog = new HashMap<String, String>();
HashMap<String,String> user = new HashMap<String, String>();
user = db.getUserDetails();
//Store the userlog by passing to UserLogEntry
userid = user.get("uid");
if(userid != ""){
userLog = db.getNewestUserLogEntryForMed(medname);
String tabstr = userLog.get("tabsleft");
Log.d ("getNewestUserLogEntryForMed()", "medname " + userLog.get("medname") + " timetaken "
+ userLog.get("medtaken") + " Tabs - " + tabstr);
int tabs = Integer.parseInt(tabstr);
tabs = tabs - 1;
String tabsupdated = Integer.toString(tabs);
Log.d ("Tabs Updated - ", "medname " + userLog.get("medname") + " timetaken "
+ userLog.get("medtaken") + " Tabs - " + tabsupdated);
UserLogEntry userlog = new UserLogEntry(getApplicationContext(),userid,medname,tabsupdated);
userlog.addUserLog();
finish();
}
}
}
}
任何提示?
非常感谢
答案 0 :(得分:0)
所以答案就是盯着我。我可以使用bundle将我需要的任何内容传递给警报意图。我已经传递了userid(错误)。只要有意图,我就必须记住,我可以使用bundle来传递简单的变量。
Intent intentAlarm = new Intent(c,AlarmReceiver.class);
Bundle extras = new Bundle();
extras.putString("userid", userid);
extras.putInt("alarmID", alarmID);
extras.putString("userlogID", userlogID);
extras.putString("medname", medname);
intentAlarm.putExtras(extras);
我想我会回答我自己的问题以防其他人需要做类似的事情,但如果这没有用,主持人可以删除它。