国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > Android开发:利用AlarmManager不间断向服务器发送请求以及notification通知

Android开发:利用AlarmManager不间断向服务器发送请求以及notification通知

来源:程序员人生   发布时间:2016-06-07 17:30:15 阅读次数:2542次

1.前言

嗯,其实需求很简单,但是由于服务器不会主动联系客户端,所以客户端必须不中断的向服务器要求以便得到1些数据,突然不知道怎样描写这个问题了,总之,我是通过AlarmManager来实现客户端不断地向服务器发送要求,好吧,往下。

2.实现

客户端不断的发要求,然后通过取得的响应做1些处理就能够了,流程就简简单单的像下面这个图。
这里写图片描述

第1步:利用AlarmManager开启轮询服务

public class MyAlarmManager { //开启轮询服务 public static void startPollingService(Context context, int seconds, Class<?> cls,String carUserId) { //获得AlarmManager系统服务 AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Log.e("startPollingService:","开启轮询服务"); Intent intent = new Intent(context, cls); intent.putExtra("carUserId",carUserId);//添加需要传递的1些参数 PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);//我是用的是service //使用AlarmManger的setRepeating方法设置定期履行的时间间隔(seconds秒)和需要履行的Service manager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), seconds * 1000, pendingIntent); } //停止轮询服务 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); //取消正在履行的服务 manager.cancel(pendingIntent); } }

第2步:Service/BroadcastReceiver/Activity完成相应的要求

我使用的是Service

/** *轮询服务 *使用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); //要求数据 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()来替换 notification.flags |= Notification.FLAG_AUTO_CANCEL; // FL manager.notify(1, notification); } @Override public void onDestroy() { super.onDestroy(); System.out.println("Service:onDestroy"); } }

大功告成

3.问题

大家在开发的进程中可能会发现1些问题,比如
1.不中断轮询失败
这里1定要注意 manager.setRepeating()的参数,特别是第1个参数和第2个参数相对应,即关于闹铃的类型问题:

//1共有5种闹铃类型: public static final int ELAPSED_REALTIME //当系统进入眠眠状态时,这类类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是相对时间,是从系统启动后开始计时的,包括睡眠时间,可以通过调用SystemClock.elapsedRealtime()取得。系统值是3 (0x00000003)。 public static final int ELAPSED_REALTIME_WAKEUP //能唤醒系统,用法同ELAPSED_REALTIME,系统值是2 (0x00000002) ublic static final int RTC //当系统进入眠眠状态时,这类类型的闹铃不会唤醒系统。直到系统下次被唤醒才传递它,该闹铃所用的时间是绝对时间,所用时间是UTC时间,可以通过调用 System.currentTimeMillis()取得。系统值是1 (0x00000001) 。 public static final int RTC_WAKEUP //能唤醒系统,用法同RTC类型,系统值为 0 (0x00000000) 。 Public static final int POWER_OFF_WAKEUP //能唤醒系统,它是1种关机闹铃,就是说装备在关机状态下也能够唤醒系统,所以我们把它称之为关机闹铃。使用方法同RTC类型,系统值为4(0x00000004)。

2.Notification不提示消息的问题
1).请设置icon
2).如果API是16请将getNotification()换成build()

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生