自制闹钟,响铃时间总是比设置时间晚3分钟左右?咋回事?

再简单不过的练手项目给我整郁闷了,现在的问题时App可以运行,假如我设置闹铃时间为9:00,那App到了9:00它不响铃,偏偏到了9:03才响铃,这是咋回事?我用的真机测试的,系统时间保证没有错误,时区设置也没有错误。

感谢各位前辈指点

下面是全部代码:
MainActivity.java

public class MainActivity extends Activity {

private TimePicker timePicker;
private TextView tv_time;
private Button btn_setClock;
private AlarmManager alarmManager;
int hour;
int min;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

timePicker = (TimePicker) findViewById(R.id.timePicker);
tv_time = (TextView) findViewById(R.id.tv_time);
btn_setClock = (Button) findViewById(R.id.btn_setClock);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
hour = hourOfDay;
min = minute;
tv_time.setText(hour + " : " + min);
}
});

btn_setClock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(MainActivity.this,
AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(MainActivity.this, "ok", 1000).show();
}
});
}
}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Debug", "onReceive");
Intent i = new Intent(context, AlarmActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}

AlarmActivity.java

public class AlarmActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AlarmActivity.this);
alertDialog.setTitle("闹钟");
alertDialog.setMessage("时间到了!");
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlarmActivity.this.finish();
}
});
alertDialog.show();
}
}

AndroidManifest.xml

        ...
        
        
        
                     android:name=".AlarmReceiver"
            android:process=":remote" />
    

你可能感兴趣的:(自制闹钟,响铃时间总是比设置时间晚3分钟左右?咋回事?)