2016-08-23 93 views
0

我對android開發感興趣,我想知道是否有可能在特定的一天在特定的時間在Android的特定時間播放一些歌曲,以及如何去做。謝謝!AlarmClock只有一次,每年在特定的一天在特定的時間

public class AlarmActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_alarm); 

    Intent myIntent = new Intent(AlarmActivity.this , MyBroadcastReceiver.class); 
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    PendingIntent pendingIntent = PendingIntent.getService(AlarmActivity.this, 0, myIntent, 0); 
    Calendar cal = Calendar.getInstance(); 
    cal.set(Calendar.HOUR_OF_DAY, 02); 
    cal.set(Calendar.MINUTE, 18); 
    cal.set(Calendar.SECOND, 55); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60000 , pendingIntent); //set repeating every year 


} 

}

廣播:

public class MyBroadcastReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "Broadcast Received.", Toast.LENGTH_LONG).show(); 
     // Vibrate the mobile phone 
     Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 
     vibrator.vibrate(2000); 
    } 
} 
+0

這將幫助你 - '的https:// developer.android.com /引導/ index.html'和'https://developer.android.com/reference/android/app/AlarmManager.html' –

回答

0
Intent myIntent = new Intent(MainActivity.this , NotifyService.class);  
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0); 
Calendar cal = Calendar.getInstance(); 
cal.set(Calendar.HOUR_OF_DAY, YOUR_HOUR); 
cal.set(Calendar.MINUTE, 00); 
cal.set(Calendar.SECOND, 00); 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 31556952000 , pendingIntent); //set repeating every year 

希望這有助於..

+0

謝謝,但你能告訴我我做錯了什麼嗎?請參閱編輯的文章 – Mifo

+0

對不起我的錯,這是工作:) – Mifo

+0

感謝兄弟.. upvote;) –

0

檢查official documentation學習如何設置鬧鐘。

至於你的具體目標,你可以在當前的一個響鈴時爲下一年設置一個新的警報。

相關問題