1. 程式人生 > >Android之實現鬧鐘

Android之實現鬧鐘

1.佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設定鬧鐘一次" android:onClick="setAlarmone"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設定鬧鐘週期"
android:onClick="setAlarmcycle"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消設定鬧鐘週期" android:onClick="mcycle"/> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_ring" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="起床了" android:textSize="30sp"/> <TimePicker android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/timePicker2" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="關閉鬧鐘" android:onClick="close"/> </LinearLayout> 2.MainActivity .java程式碼 public class MainActivity extends AppCompatActivity { private AlarmManager alarmManager; private PendingIntent pendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取鬧鐘管理者 alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); } public void setAlarmone(View view) { Calendar calendar = Calendar.getInstance(); final int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); Intent intent = new Intent(); intent.setAction("com.zking.g158231_android28.RING"); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0x100, intent, 0); alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent); } }, hour, minute, true); timePickerDialog.show(); } public void setAlarmcycle(View view) { Calendar calendar = Calendar.getInstance(); final int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); Intent intent = new Intent(); intent.setAction("com.zking.g158231_android28.RING"); pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0x102, intent, 0); //每5秒掉一次鬧鐘 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),5000, pendingIntent); } }, hour, minute, true); timePickerDialog.show(); } public void mcycle(View view){ alarmManager.cancel(pendingIntent); } } 3.廣播接受著程式碼 public class Ring extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("com.zking.g158231_android28.RING".equals(intent.getAction())){ Intent intent1=new Intent(context,RingActivity.class); intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent1); } } } 4.鬧鐘頁面程式碼 public class RingActivity extends AppCompatActivity { private MediaPlayer mediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ring); //播放音樂 mediaPlayer = MediaPlayer.create(this, R.raw.aaa); mediaPlayer.start(); } public void close(View view){ mediaPlayer.stop(); finish(); } } 5.在清單檔案application下注冊廣播 <receiver android:name=".Ring"> <intent-filter> <action android:name="com.zking.g158231_android28.RING"></action> </intent-filter> </receiver>