代码没有报错,设置好闹铃时间之后延迟将近50秒才有反应~请问这是什么回事?我把我代码贴出来,希望大神们能指导一下~~
package com.android.app;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
public class AlarmTestActivity extends Activity {
Button setTime;
AlarmManager aManager;
Calendar currentTime = Calendar.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTime = (Button) findViewById(R.id.setTime);
//获取AlarmManager对象
aManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
setTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 创建一个TimePickerDialog,并把它显示出来
new TimePickerDialog(AlarmTestActivity.this, 0, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar currentTime = Calendar.getInstance();
//启动指定AlarmActivity组件
Intent intent = new Intent(AlarmTestActivity.this,AlarmActivity.class);
//创建PendingIntent对象
PendingIntent pi = PendingIntent.getActivity(AlarmTestActivity.this, 0, intent, 0);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
//根据用户选择的时间来设置Calendar对象
c.set(Calendar.HOUR, hourOfDay);
c.set(Calendar.MINUTE, minute);
//设置AlarmManager将在Calender对应的时间启动指定组件
aManager.set(AlarmManager.RTC, c.getTimeInMillis(), pi);
Toast.makeText(AlarmTestActivity.this, "闹铃设置成功!", Toast.LENGTH_SHORT).show();
}
}, currentTime.get(Calendar.HOUR_OF_DAY), currentTime.get(Calendar.MINUTE),false).show();
}
});
}
}
package com.android.app;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
public class AlarmActivity extends Activity {
MediaPlayer alarmMusic;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
alarmMusic = MediaPlayer.create(this, R.raw.jasmine);
alarmMusic.setLooping(true);
alarmMusic.start();
// 创建一个对话框
new AlertDialog.Builder(AlarmActivity.this).setTitle("闹铃")
.setMessage("闹铃响了!!!Come On!")
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
alarmMusic.stop();
AlarmActivity.this.finish();
}
}).show();
}
}
------解决思路----------------------
aManager.set(AlarmManager.RTC,c.getTimeInMillis(), pi);
你输出下这个getTimeInMillis,貌似是时区问题
------解决思路----------------------
你没发现时间不对么……你的截图是星期2 11:30分?现在才刚星期二吧