2016-04-28 53 views
-3

我設置了鬧鐘並設置了開始時間,但鬧鐘開始延遲了幾分鐘或幾秒鐘。我在android 4.4+上使用setWindow這是沒有用的,如何使它在正確的時間開始。設置安卓鬧鐘,並沒有在正確的時間顯示

public class AlarmSetActivity extends Activity { 

private Context context; 
private AlarmManager manager; 
private Intent intent; 
private PendingIntent pi; 
@ViewInject(R.id.activity_content_tv) 
private TextView title; 
@ViewInject(R.id.time_set_btn) 
private Button timeSetBtn; 
@ViewInject(R.id.time_set_open_che) 
private CheckBox timeSetOpenCheck; 

@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; 
    manager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    title.setText("鬧鐘設置 "); 
    intent = new Intent(context, AlarmNotifyReceiver.class); 
    pi = PendingIntent.getBroadcast(context, 0, intent, 0); 
    if (!timeSetOpenCheck.isChecked()) { 
     manager.cancel(pi); 
    } 
} 

@OnClick({ R.id.time_set_btn, R.id.alarm_voice_set, R.id.back_btn, 
     R.id.voice_set_btn }) 
public void onClick(View view) { 
    switch (view.getId()) { 
    case R.id.time_set_btn: 
     if (timeSetOpenCheck.isChecked()) { 
      setTime(); 
     } 
     break; 
    case R.id.back_btn: 
     onBackPressed(); 
     break; 
    case R.id.alarm_voice_set: 
     setAlarmRingtone(); 
     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_ALARM, uri); 
     } 
     break; 

    default: 
     break; 
    } 
} 

/** 
* 鬧鐘的時間選擇 
*/ 
private void setTime() { 
    // 彈出設置時間的窗口 
    new TimeDialog(context).setTime(timeSetBtn); 
} 

/** 
* 選擇鬧鐘鈴聲 
*/ 
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); 
} 
+0

如果您希望它在特定時間觸發,請使用'AlarmManager#setExact()'方法。 –

+0

仍然沒有完全運行 – user5607014

+0

然後提供給我們一個[mcve]。沒有人想挖掘代碼頁來找出原因。 –

回答

0

您沒有設置第二個。因此,警報將在您設置的時間的當前秒開始,並且似乎被延遲

+0

但有時它會在幾分鐘後開始.... – user5607014