嗯,其實(shí)需求很簡(jiǎn)單,但是由于服務(wù)器不會(huì)主動(dòng)聯(lián)系客戶(hù)端,所以客戶(hù)端必須不中斷的向服務(wù)器要求以便得到1些數(shù)據(jù),突然不知道怎樣描寫(xiě)這個(gè)問(wèn)題了,總之,我是通過(guò)AlarmManager來(lái)實(shí)現(xiàn)客戶(hù)端不斷地向服務(wù)器發(fā)送要求,好吧,往下。
客戶(hù)端不斷的發(fā)要求,然后通過(guò)取得的響應(yīng)做1些處理就能夠了,流程就簡(jiǎn)簡(jiǎn)單單的像下面這個(gè)圖。
public class MyAlarmManager
{
//開(kāi)啟輪詢(xún)服務(wù)
public static void startPollingService(Context context, int seconds, Class<?> cls,String carUserId) {
//獲得AlarmManager系統(tǒng)服務(wù)
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Log.e("startPollingService:","開(kāi)啟輪詢(xún)服務(wù)");
Intent intent = new Intent(context, cls);
intent.putExtra("carUserId",carUserId);//添加需要傳遞的1些參數(shù)
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);//我是用的是service
//使用AlarmManger的setRepeating方法設(shè)置定期履行的時(shí)間間隔(seconds秒)和需要履行的Service
manager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), seconds * 1000, pendingIntent);
}
//停止輪詢(xún)服務(wù)
public static void stopPollingService(Context context, Class<?> cls,String action) {
AlarmManager manager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, cls);
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
//取消正在履行的服務(wù)
manager.cancel(pendingIntent);
}
}
我使用的是Service
/**
*輪詢(xún)服務(wù)
*使用notification彈出消息
*/
public class QueryUnusualService extends Service {
private Notification notification;
private NotificationManager manager;
private Handler handler;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
@Override
public void onCreate() {
super.onCreate();
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what)
{
case R.id.query_unusual_car_result:
List<Map<String,Object>> unusualCarList = (List<Map<String,Object>>)msg.getData().getSerializable("unusualCarList");
if(unusualCarList==null||unusualCarList.size()<1)
return;
showNotification(unusualCarList);
break;
default:
break;
}
}
};
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
//要求數(shù)據(jù)
CarController.queryUnusualCar(intent.getStringExtra("carUserId"), handler);
}
//彈出Notification
private void showNotification(List<Map<String,Object>> unusualCarList) {
final Bitmap largeIcon = ((BitmapDrawable) getResources().getDrawable(R.drawable.stefan)).getBitmap();
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0, new Intent().setAction("intvehapp.intvehapp.Activity.BaiDuMapActivity"), 0);
notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.head_image)
.setLargeIcon(largeIcon)
.setTicker("新消息!!")
.setContentTitle("新消息!!!!")
.setContentText("新消息~")
.setContentIntent(pendingIntent3).setNumber(1).getNotification(); // 需要注意build()是在API
// level16及以后增加的,API11可使用getNotificatin()來(lái)替換
notification.flags |= Notification.FLAG_AUTO_CANCEL; // FL
manager.notify(1, notification);
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Service:onDestroy");
}
}
大功告成
大家在開(kāi)發(fā)的進(jìn)程中可能會(huì)發(fā)現(xiàn)1些問(wèn)題,比如
1.不中斷輪詢(xún)失敗
這里1定要注意 manager.setRepeating()的參數(shù),特別是第1個(gè)參數(shù)和第2個(gè)參數(shù)相對(duì)應(yīng),即關(guān)于鬧鈴的類(lèi)型問(wèn)題:
//1共有5種鬧鈴類(lèi)型:
public static final int ELAPSED_REALTIME
//當(dāng)系統(tǒng)進(jìn)入眠眠狀態(tài)時(shí),這類(lèi)類(lèi)型的鬧鈴不會(huì)喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時(shí)間是相對(duì)時(shí)間,是從系統(tǒng)啟動(dòng)后開(kāi)始計(jì)時(shí)的,包括睡眠時(shí)間,可以通過(guò)調(diào)用SystemClock.elapsedRealtime()取得。系統(tǒng)值是3 (0x00000003)。
public static final int ELAPSED_REALTIME_WAKEUP //能喚醒系統(tǒng),用法同ELAPSED_REALTIME,系統(tǒng)值是2 (0x00000002)
ublic static final int RTC
//當(dāng)系統(tǒng)進(jìn)入眠眠狀態(tài)時(shí),這類(lèi)類(lèi)型的鬧鈴不會(huì)喚醒系統(tǒng)。直到系統(tǒng)下次被喚醒才傳遞它,該鬧鈴所用的時(shí)間是絕對(duì)時(shí)間,所用時(shí)間是UTC時(shí)間,可以通過(guò)調(diào)用 System.currentTimeMillis()取得。系統(tǒng)值是1 (0x00000001) 。
public static final int RTC_WAKEUP
//能喚醒系統(tǒng),用法同RTC類(lèi)型,系統(tǒng)值為 0 (0x00000000) 。
Public static final int POWER_OFF_WAKEUP
//能喚醒系統(tǒng),它是1種關(guān)機(jī)鬧鈴,就是說(shuō)裝備在關(guān)機(jī)狀態(tài)下也能夠喚醒系統(tǒng),所以我們把它稱(chēng)之為關(guān)機(jī)鬧鈴。使用方法同RTC類(lèi)型,系統(tǒng)值為4(0x00000004)。
2.Notification不提示消息的問(wèn)題
1).請(qǐng)?jiān)O(shè)置icon
2).如果API是16請(qǐng)將getNotification()換成build()