我對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);
}
}
這將幫助你 - '的https:// developer.android.com /引導/ index.html'和'https://developer.android.com/reference/android/app/AlarmManager.html' –