在mainactivity里设置了pendingintent,设置好闹钟的启动时间和铃声,由此打开一个新的activity,在这个新的activity中执行铃声的播放和闹钟提醒,但是没有任何反应
public class AlarmSetActivity extends Activity {
private Context context;
private AlarmManager manager;
@ViewInject(R.id.activity_content_tv)
private TextView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_set);
ViewUtils.inject(this);
context = this;
title.setText("闹钟设置 ");
manager = (AlarmManager) getSystemService(ALARM_SERVICE);
}
@OnClick({ R.id.time_set_btn, R.id.voice_set_btn, R.id.back_btn })
public void onClick(View view) {
switch (view.getId()) {
case R.id.time_set_btn:
setTime();
break;
case R.id.back_btn:
onBackPressed();
break;
case R.id.voice_set_btn:
setAlarmRingtone();
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
Uri uri = data
.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
RingtoneManager.setActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_RINGTONE, uri);
}
break;
default:
break;
}
}
/**
* 闹钟的时间选择
*/
private void setTime() {
// 当前设备上的系统时间
Calendar cal = Calendar.getInstance();
// 弹出设置时间的窗口
new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Intent intent = new Intent(context, AlarmNotifyActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0,
intent, 0);
Calendar setCal = Calendar.getInstance();
setCal.set(Calendar.HOUR_OF_DAY, hourOfDay);
setCal.set(Calendar.MINUTE, minute);
// 设置AlarmManager将在Calendar对应的时间启动指定组件
// 将在指定的时间(24h)之后再次启动
manager.setRepeating(AlarmManager.RTC_WAKEUP,
setCal.getTimeInMillis(), 1000 * 60 * 60 * 24, pi);
Toast.makeText(context, "闹铃设置成功", Toast.LENGTH_SHORT).show();
}
}, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true)
.show();
}
/**
* 选择闹钟铃声
*/
private void setAlarmRingtone() {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
RingtoneManager.TYPE_ALARM);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "请选择闹钟铃声");
startActivityForResult(intent, 1);
}
}
第二个activity
public class AlarmNotifyActivity extends Activity {
private MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
player = MediaPlayer.create(this, RingtoneManager
.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM));
player.setLooping(true);
player.start();
new AlertDialog.Builder(AlarmNotifyActivity.this).setTitle("测量提醒")
.setMessage("请起床进行体温测量!")
.setPositiveButton("好哒~~", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 停止音乐播放
player.stop();
// 结束该Activity
AlarmNotifyActivity.this.finish();
}
}).show();
}
}
二十四小時過去了,可能你的程式進程已經被系統殺死了,自然就不會有任何反應了。你可以先把時間設定短一點(如一兩分鐘),因為你只有activity,所以最好再保持你的activity在前台運行,沒有在這一兩分鐘內被銷毀,這樣你就可以驗證一下你程序的正確與否。要是這一兩分鐘後鬧鈴響起,你就再考慮怎麼讓程式進程不會因為長時間不動而被系統殺死的問題了。光靠兩個activity來維持那麼長時間是不可靠的。