在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来保持那么长时间是不靠谱的。