現在普遍的手機都會有一個鬧鐘的功能,如果使用Android來實現一個鬧鐘可以使用AtarmManager來實現。AtarmManager提供了一種系統級的提示服務,允許你安排在將來的某個時間執行一個服務。AlarmManager對象一般不直接實例化,而是通過Context.getsystemservice(Context.ALARM_SERVICE)方法獲得。
下面是我們運用之前用到的TimePickerDialog結合AlarmManager,實現一個可以設定任意時間而且可以重復的鬧鐘,演示效果如下(大概每晚23:48會有鬧鐘通知):
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/activity_main" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 tools:context="com.example.administrator.alarmdemo.MainActivity"> 10 <TextView 11 android:id="@+id/time_tv" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:hint="請設置鬧鈴時間" 15 android:clickable="true" 16 android:onClick="setTime" 17 android:gravity="center"/> 18 <TextView 19 android:id="@+id/time_tv2" 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:hint="請設置響鈴間隔時間" 23 android:clickable="true" 24 android:onClick="setIntervalTime" 25 android:gravity="center"/> 26 <Button 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:onClick="open" 30 android:text="開啟鬧鐘"/> 31 <Button 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content" 34 android:onClick="stop" 35 android:text="結束鬧鐘"/> 36 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.administrator.alarmdemo"> 4 <application 5 android:allowBackup="true" 6 android:icon="@mipmap/ic_launcher" 7 android:label="@string/app_name" 8 android:supportsRtl="true" 9 android:theme="@style/AppTheme"> 10 <activity android:name=".MainActivity"> 11 <intent-filter> 12 <action android:name="android.intent.action.MAIN" /> 13 14 <category android:name="android.intent.category.LAUNCHER" /> 15 </intent-filter> 16 </activity> 17 <receiver 18 android:name=".RepeatAlarmReceiver" 19 android:enabled="true" 20 android:exported="true"> 21 <intent-filter> 22 <action android:name="repeatAlarm" /> 23 </intent-filter> 24 </receiver> 25 <activity android:name=".Main2Activity"></activity> 26 </application> 27 28 </manifest>
1 import android.content.BroadcastReceiver; 2 import android.content.Context; 3 import android.content.Intent; 4 import android.widget.Toast; 5 6 public class RepeatAlarmReceiver extends BroadcastReceiver { 7 public RepeatAlarmReceiver() { 8 } 9 @Override 10 public void onReceive(Context context, Intent intent) { 11 Toast.makeText(context, "重復鬧鐘", Toast.LENGTH_SHORT).show(); 12 } 13 }
1 import android.app.AlarmManager; 2 import android.app.PendingIntent; 3 import android.app.TimePickerDialog; 4 import android.content.Intent; 5 import android.provider.Settings; 6 import android.support.v7.app.AppCompatActivity; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.TextView; 10 import android.widget.TimePicker; 11 import java.util.Calendar; 12 public class MainActivity extends AppCompatActivity { 13 AlarmManager alarmManagerSet; 14 PendingIntent pendingIntentSet; 15 int myHour,myMinute;//聲明響鈴時間 16 int intervalHour,intervalMinute,intervalSecond;//聲明間隔時間(響鈴周期) 17 Calendar calendar;//聲明日歷 18 TextView time_tv,time_tv2; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 alarmManagerSet= (AlarmManager) getSystemService(ALARM_SERVICE); 25 //設置聲明時間格式 26 calendar = Calendar.getInstance(); 27 myHour=calendar.get(Calendar.HOUR_OF_DAY); 28 myMinute=calendar.get(Calendar.MINUTE); 29 intervalHour=calendar.get(Calendar.HOUR_OF_DAY); 30 intervalMinute=calendar.get(Calendar.MINUTE); 31 //獲取時間設置的TextView 32 time_tv= (TextView) findViewById(R.id.time_tv); 33 time_tv2= (TextView) findViewById(R.id.time_tv2); 34 } 35 //使用timePickerDialog設置鬧鈴響鈴時間 36 public void setTime(View view){ 37 TimePickerDialog timePickerDialog =new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { 38 @Override 39 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 40 myHour=hourOfDay; 41 myMinute=minute; 42 time_tv.setText(myHour+":"+myMinute); 43 } 44 },myHour,myMinute,true); 45 timePickerDialog.show(); 46 } 47 //使用timePickerDialog設置鬧鈴響鈴周期(間隔) 48 public void setIntervalTime(View view){ 49 TimePickerDialog timePickerDialog =new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { 50 @Override 51 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 52 intervalHour=hourOfDay; 53 intervalMinute=minute; 54 time_tv2.setText(intervalHour+":"+intervalMinute); 55 } 56 },intervalHour,intervalMinute,true); 57 timePickerDialog.show(); 58 } 59 public void open(View view){ 60 //分別獲取鬧鈴響鈴時間以及鬧鈴間隔時間(用冒號分割存儲於數組中) 61 String[] triggerTime=time_tv.getText().toString().split(":"); 62 String[] intervalTime=time_tv2.getText().toString().split(":"); 63 //通過Calendar分別獲取對應的小時和分鐘(需要將String轉換為int) 64 Calendar calendar1 = Calendar.getInstance(); 65 calendar1.set(Calendar.HOUR_OF_DAY,Integer.parseInt(triggerTime[0])); 66 calendar1.set(Calendar.MINUTE,Integer.parseInt(triggerTime[1])); 67 //得到點擊觸發的毫秒值(即鬧鐘提醒時間) 68 long triggerAtMillis= calendar1.getTimeInMillis(); 69 //設定pendinngIntent接受自定義鬧鈴廣播 70 pendingIntentSet = PendingIntent.getBroadcast(this,0,new Intent("repeatAlarm"),0); 71 //判斷如果當前系統時間大於設置的鬧鈴時間,則在第二天開始啟用該鬧鈴 72 if(System.currentTimeMillis()>triggerAtMillis){ 73 triggerAtMillis=triggerAtMillis+24*60*60*1000; 74 } 75 //使用.setRepeating設置重復鬧鈴 76 alarmManagerSet.setRepeating(AlarmManager.RTC_WAKEUP,triggerAtMillis,intervalMillis,pendingIntentSet); 77 } 78 public void stop(View v){ 79 alarmManagerSet.cancel(pendingIntentSet); 80 } 81 }
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11