編輯:關於Android編程
本節引言:
本節主要介紹的是Android系統服務中的---AlarmManager(鬧鐘服務),
除了開發手機鬧鐘外,更多的時候是作為一個全局的定時器,通常與Service
結合,在特定時間啟動其他的組件!本節就來對這個AlarmManager來進行解析
同時通過小鬧鐘與自動換壁紙來演示這個AlarmManager的用法,好了,開始本節的
內容吧!
本節正文:
好了,光看不練可不行,下面就來寫兩個簡單的例子:
分別是定時鬧鐘提醒的實現與定時更換手機壁紙,一個是調用Activity,一個是調用Service
MainActivity.java:
package com.jay.example.alarmmanagerdemo; import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity { private Button btnSetClock; private Button btnbtnCloseClock; private AlarmManager alarmManager; private PendingIntent pi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSetClock = (Button) findViewById(R.id.btnSetClock); btnbtnCloseClock = (Button) findViewById(R.id.btnCloseClock); // ①獲取AlarmManager對象: alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); // 指定要啟動的是Activity組件,通過PendingIntent調用getActivity來設置 Intent intent = new Intent(MainActivity.this, ClockActivity.class); pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); btnSetClock.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Calendar currentTime = Calendar.getInstance(); // 彈出一個時間設置的對話框,供用戶選擇時間 new TimePickerDialog(MainActivity.this, 0, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { //設置當前時間 Calendar c = Calendar.getInstance(); c.setTimeInMillis(System.currentTimeMillis()); // 根據用戶選擇的時間來設置Calendar對象 c.set(Calendar.HOUR, hourOfDay); c.set(Calendar.MINUTE, minute); // ②設置AlarmManager在Calendar對應的時間啟動Activity alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); // 提示鬧鐘設置完畢: Toast.makeText(MainActivity.this, 鬧鐘設置完畢~, Toast.LENGTH_SHORT).show(); } }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime .get(Calendar.MINUTE), false).show(); btnbtnCloseClock.setVisibility(View.VISIBLE); } }); btnbtnCloseClock.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { alarmManager.cancel(pi); btnbtnCloseClock.setVisibility(View.GONE); Toast.makeText(MainActivity.this, 鬧鐘已取消, Toast.LENGTH_SHORT) .show(); } }); } }
ClockActivity.java:
package com.jay.example.alarmmanagerdemo; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.media.MediaPlayer; import android.os.Bundle; public class ClockActivity extends Activity { private MediaPlayer mediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_clock); mediaPlayer = MediaPlayer.create(this, R.raw.pig); //mediaPlayer.setLooping(true); mediaPlayer.start(); //創建一個鬧鐘提醒的對話框,點擊確定關閉鈴聲與頁面 new AlertDialog.Builder(ClockActivity.this).setTitle(鬧鐘).setMessage(小豬小豬快起床~) .setPositiveButton(關閉鬧鈴, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mediaPlayer.stop(); ClockActivity.this.finish(); } }).show(); } }
運行截圖:
總結核心流程吧:
①AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
獲得系統提供的AlarmManager服務的對象
②Intent設置要啟動的組件:
Intent intent = new Intent(MainActivity.this, ClockActivity.class);
③PendingIntent對象設置動作,啟動的是Activity還是Service,又或者是廣播!
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
④調用AlarmManager的set( )方法設置單次鬧鐘的鬧鐘類型,啟動時間以及PendingIntent對象!
alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(), pi);
從上面的代碼我們發現AlarmManager的用法其實就那麼幾步,很簡單,接下來我們弄一個Service
這裡我們涉及到另外一個API: WallpaperManager,管理壁紙的,提供了下述幾個方法供我們使用:
setBitmap(Bitmap bitmap),setResource(int resid),setStream(InputStream data);
還有一個清除壁紙的clean方法!
流程和前面的鬧鐘原理差不多,就不講解了,直接貼代碼吧:
activity_main.xml:
package com.example.alarmmanagerdemo2; import java.io.IOException; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.WallpaperManager; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button btnOn; private Button btnOff; private Button btnCancel; private AlarmManager alarmManager; private PendingIntent pi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //①獲得AlarmManager對象: alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);; //②指定要啟動的Service,並指明動作是Servce: Intent intent = new Intent(MainActivity.this,WallPagerService.class); pi = PendingIntent.getService(MainActivity.this, 0, intent, 0); btnOn = (Button) findViewById(R.id.btnOn); btnOff = (Button) findViewById(R.id.btnOff); btnCancel = (Button) findViewById(R.id.btnCancel); btnOn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 3000, pi); btnOn.setEnabled(false); btnOff.setEnabled(true); Toast.makeText(MainActivity.this, 自動更換壁紙設置成功, Toast.LENGTH_SHORT).show(); } }); btnOff.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { btnOn.setEnabled(true); btnOff.setEnabled(false); alarmManager.cancel(pi); } }); btnCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { WallpaperManager.getInstance(getApplicationContext()).clear(); Toast.makeText(MainActivity.this, 清除壁紙成功~, Toast.LENGTH_SHORT).show(); } catch (IOException e) {e.printStackTrace();} } }); } }
package com.example.alarmmanagerdemo2; import android.app.Service; import android.app.WallpaperManager; import android.content.Intent; import android.os.IBinder; public class WallPagerService extends Service { //定義當前壁紙下表 int current = 0; //獲取壁紙資源id數組 private int[] paper = new int[] { R.drawable.gui_1, R.drawable.gui_2, R.drawable.gui_3, R.drawable.gui_4, }; //定義WallpaperManager服務 private WallpaperManager wManager; //初始化WallPagerManager @Override public void onCreate() { super.onCreate(); wManager = WallpaperManager.getInstance(this); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if(current >= 4)current = 0; try{ wManager.setResource(paper[current++]); }catch(Exception e){e.printStackTrace();} return START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } }
運行效果圖:
最後說兩句:
就是這麼簡單,關於這個AlarmManager(鬧鐘服務)就學習到這裡吧,在後面我們在Service實例講解
處還會做一個後台定時發送短信的例子,敬請期待~
在Android開發中,我們經常會遇到這樣一種情況:在UI界面上進行某項操作後要執行一段很耗時的代碼,比如我們在界面上點擊了一個”下載“按鈕,那麼
JPush嵌入工程官網詳解自定義通知欄先自定義Receiver
Android模擬器報Failed To Allocate memory 8錯誤的解決辦法Android在啟動模擬器AVD時,出現下面的異常:“Failed to all
購物車最近寫了一個商城APP中常用功能“購物車”購物車效果圖如下:購物車功能需求如下:下面是購物車主activity“MainActi