android闹钟测试工具,android开发:AlarmManager闹钟管理器的实例
1、AlarmManager,顧名思義,就是“提醒”,是Android中常用的一種系統(tǒng)級別的提示服務(wù),在特定的時刻為我們廣播一個指定的PendingIntent。通俗點,就是設(shè)置一個時間,在指定的時間里,Alarm會幫我們執(zhí)行PendingIntent里的廣播。
2、AlarmManager的常用方法有三個:
(1)set(int type,long startTime,PendingIntent pi);
該方法用于設(shè)置一次性鬧鐘,第一個參數(shù)表示鬧鐘類型,第二個參數(shù)表示鬧鐘執(zhí)行時間,第三個參數(shù)表示鬧鐘響應(yīng)動作。
(2)setRepeating(int type,long startTime,long intervalTime,PendingIntent pi);
該方法用于設(shè)置重復(fù)鬧鐘,第一個參數(shù)表示鬧鐘類型,第二個參數(shù)表示鬧鐘首次執(zhí)行時間,第三個參數(shù)表示鬧鐘兩次執(zhí)行的間隔時間,第三個參數(shù)表示鬧鐘響應(yīng)動作。
(3)setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi);
該方法也用于設(shè)置重復(fù)鬧鐘,與第二個方法相似,不過其兩個鬧鐘執(zhí)行的間隔時間不是固定的而已。
3、三個方法各個參數(shù)詳悉:
(1)int type: 鬧鐘的類型,常用的有5個值:AlarmManager.ELAPSED_REALTIME、 AlarmManager.ELAPSED_REALTIME_WAKEUP、AlarmManager.RTC、 AlarmManager.RTC_WAKEUP、AlarmManager.POWER_OFF_WAKEUP。
AlarmManager.ELAPSED_REALTIME表示鬧鐘在手機睡眠狀態(tài)下不可用,該狀態(tài)下鬧鐘使用相對時間(相對于系統(tǒng)啟動開始),狀態(tài)值為3;
AlarmManager.ELAPSED_REALTIME_WAKEUP表示鬧鐘在睡眠狀態(tài)下會喚醒系統(tǒng)并執(zhí)行提示功能,該狀態(tài)下鬧鐘也使用相對時間,狀態(tài)值為2;
AlarmManager.RTC表示鬧鐘在睡眠狀態(tài)下不可用,該狀態(tài)下鬧鐘使用絕對時間,即當(dāng)前系統(tǒng)時間,狀態(tài)值為1;
AlarmManager.RTC_WAKEUP表示鬧鐘在睡眠狀態(tài)下會喚醒系統(tǒng)并執(zhí)行提示功能,該狀態(tài)下鬧鐘使用絕對時間,狀態(tài)值為0;
AlarmManager.POWER_OFF_WAKEUP表示鬧鐘在手機關(guān)機狀態(tài)下也能正常進行提示功能,所以是5個狀態(tài)中用的最多的狀態(tài)之一,該狀態(tài)下鬧鐘也是用絕對時間,狀態(tài)值為4;不過本狀態(tài)好像受SDK版本影響,某些版本并不支持;
(2)long startTime: 鬧鐘的第一次執(zhí)行時間,以毫秒為單位,可以自定義時間,不過一般使用當(dāng)前時間。需要注意的是,本屬性與第一個屬性(type)密切相關(guān),如果第一個參數(shù)對 應(yīng)的鬧鐘使用的是相對時間(ELAPSED_REALTIME和ELAPSED_REALTIME_WAKEUP),那么本屬性就得使用相對時間(相對于 系統(tǒng)啟動時間來說),比如當(dāng)前時間就表示為:SystemClock.elapsedRealtime();如果第一個參數(shù)對應(yīng)的鬧鐘使用的是絕對時間 (RTC、RTC_WAKEUP、POWER_OFF_WAKEUP),那么本屬性就得使用絕對時間,比如當(dāng)前時間就表示 為:System.currentTimeMillis()。
(3)long intervalTime:對于后兩個方法來說,存在本屬性,表示兩次鬧鐘執(zhí)行的間隔時間,也是以毫秒為單位。
(4)PendingIntent pi: 綁定了鬧鐘的執(zhí)行動作,比如發(fā)送一個廣播、給出提示等等。PendingIntent是Intent的封裝類。需要注意的是,如果是通過啟動服務(wù)來實現(xiàn)鬧鐘提 示的話,PendingIntent對象的獲取就應(yīng)該采用Pending.getService(Context c,int i,Intent intent,int j)方法;如果是通過廣播來實現(xiàn)鬧鐘提示的話,PendingIntent對象的獲取就應(yīng)該采用 PendingIntent.getBroadcast(Context c,int i,Intent intent,int j)方法;如果是采用Activity的方式來實現(xiàn)鬧鐘提示的話,PendingIntent對象的獲取就應(yīng)該采用 PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。如果這三種方法錯用了的話,雖然不會報錯,但是看不到鬧鐘提示效果。
現(xiàn)在來講解下代碼:
我們需要獲取到鬧鐘管理器的權(quán)限,以下代碼:
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
接著我們需要制定一個時間:
Calendar cal = Utils.getTimeAfterInSecs(30);//延時30秒執(zhí)行
getTimeAfterInSecs代碼:
public static Calendar getTimeAfterInSecs(int s){
Calendar cd = Calendar.getInstance();
cd.add(Calendar.SECOND,s);
return cd;
}
接著我們指定一個Intent:
Intent intent = new Intent(this,TestReceiver.class);
intent.putExtra("msg", "測試一個鬧鐘,時間是:" + Utils.getDateTimeString(cal));
同時發(fā)送一條消息,如果是開發(fā),我們可以發(fā)送我們需要的業(yè)務(wù)數(shù)據(jù)就可以了;
然后我們申明一個PendingIntent:
PendingIntent pi = PendingIntent.getBroadcast(this,1,intent,0);
第一個參數(shù)是上下文對象,第二個是請求碼,如果有多個鬧鐘,一定要區(qū)分好請求碼;
最后我們創(chuàng)一個鬧鐘:
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
如果是重復(fù)鬧鐘:
am.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),5*1000,pi);
以下我將貼出所以代碼;
MainAcitivity的代碼:
package com.lanxin.testalarm;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "AlarmLog";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button = (Button)findViewById(R.id.button);
Button button2 = (Button)findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
init();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
init2();
}
});
}
private void init() {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cal = Utils.getTimeAfterInSecs(30);
Log.i(TAG, Utils.getDateTimeString(cal));
Log.i(TAG, Utils.getNowTime());
Intent intent = new Intent(this,TestReceiver.class);
intent.putExtra("msg", "測試一個鬧鐘,時間是:" + Utils.getDateTimeString(cal));
PendingIntent pi = PendingIntent.getBroadcast(this,1,intent,0);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
}
private void init2() {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cal = Utils.getTimeAfterInSecs(30);
Calendar cal2 = Utils.getTimeAfterInSecs(15);
Log.i(TAG, Utils.getNowTime());
Intent intent = new Intent(this,TestReceiver.class);
intent.putExtra("msg", "測試一個鬧鐘,時間是:" + Utils.getDateTimeString(cal));
PendingIntent pi = PendingIntent.getBroadcast(this,1,intent,0);
Intent intent2 = new Intent(this,TestReceiver2.class);
intent2.putExtra("msg", "測試一個鬧鐘,時間是:" + Utils.getDateTimeString(cal2));
PendingIntent pi2 = PendingIntent.getBroadcast(this,2,intent2,0);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
// am.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), pi);
am.setRepeating(AlarmManager.RTC_WAKEUP,cal2.getTimeInMillis(),5*1000,pi2);
am.cancel(pi2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Receiver類的代碼:
package com.lanxin.testalarm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by Administrator on 2016/6/13 0013.
*/
public class TestReceiver extends BroadcastReceiver {
private static final String TAG = "TestReceiverLog";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, Utils.getNowTime());
Log.i(TAG,"Intent:"+intent);
String str = intent.getStringExtra("msg");
Log.i(TAG,"msg:"+str);
}
}
總結(jié)
以上是生活随笔為你收集整理的android闹钟测试工具,android开发:AlarmManager闹钟管理器的实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kaggle共享单车数据分析及预测(随机
- 下一篇: 键盘接口和七段数码管的控制实验