Android 使用Service实现不间断的网络请求
来源:互联网 发布:80端口不通怎么办 编辑:程序博客网 时间:2024/06/05 04:36
前言
Android 如何在返回到桌面后,依然在后台不间断地请求服务器?
使用前台Service
使用前台Service,可以使你的app返回到桌面后,甚至进程被杀死后,依旧会有一个Service运行在后台;在通知栏会开启一个通知,显示你的app的内容;点击通知,可以快速打开你的app;
- 创建一个Service,覆写
onCreate
;
public class NetListenService extends Service { @Override public void onCreate() { super.onCreate(); }}
- 在
onCreate
中,使用NotificationManager
创建一个通知;
NotificationManager sNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder sBuilder = new NotificationCompat .Builder(NetListenService.this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("正在监听") .setContentText("已收到警报" + notificationCount + "条"); Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class); PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); sBuilder.setContentIntent(resultPendingIntent); sNM.notify(0, sBuilder.build());
- 开启
Service
;
Intent intent = new Intent(this, NetListenService.class);startService(intent);
不间断地请求服务器
- 使用
Handler().postDelayed
开启一个延时的线程;延时时间到了以后,执行网络请求任务;开启下一个延时线程线程;
private void executeNetListen() { new Handler().postDelayed(new Runnable() { public void run() { requestServer(); executeNetListen(); } }, getIntervalTime()); }
- 使用volley来请求服务器;获取
volley
队列;
private RequestQueue mQueue;
mQueue = Volley.newRequestQueue(getApplicationContext());
- 使用
volley
请求网络;
private void requestServer() { StringRequest request = new StringRequest(Request.Method.GET, Config.URL_BASE, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d(Config.TAG, "NetListenService->onResponse: " + response); } }, new Response.ErrorListener(){ @Override public void onErrorResponse(VolleyError error) { } }); mQueue.add(request); }
修改AndroidManifest.xml
- 添加网络权限;
<uses-permission android:name="android.permission.INTERNET" />
- 注册你的service;
<service android:name=".service.NetListenService" android:enabled="true" android:exported="true"></service>
总结
- 要让你的app不被轻易的杀死,可以开启一个前台Service保留在系统中;
- 在Service中,使用延迟线程来不间断地隔一段时间做某件事情;
- 使用volley请求网络;
0 0
- Android 使用Service实现不间断的网络请求
- Android Retrofit网络请求Service
- Android使用Retrofit+OkHttp实现网络请求
- Android使用okhttpfinal+gson实现网络请求
- Android 网络请求实现
- Android HTTP网络请求的异步实现
- Android HTTP网络请求的异步实现
- Android网络请求框架Volley的使用
- android网络请求Volley框架的使用
- Android 网络请求Volley的使用
- Android网络请求 ------ Volley的使用
- Android网络请求---AsyncHttpClient的使用
- Android网络请求框架的使用okhttp
- android网络请求okhttp的使用
- Android网络请求使用Retrofit+OKHTTP,如何实现参数加密
- Android 实现网络请求单元测试
- Android使用Volley请求网络
- Android 网络请求:Retrofit 使用
- 139.Which three statements are true about multiple-row subqueries? (Choose three.)
- Android项目:手机安全卫士(8)—— 管理员权限
- Lock
- Mac下上传本地代码仓到github流程
- 使用QtCreator进行Linux内核开发
- Android 使用Service实现不间断的网络请求
- Linux crontab 计划命令
- [LeeCode]116. Populating Next Right Pointers in Each Node
- js兼容性错误导致没有办法留言
- android 正方形九宫格View设置
- [.Net码农].net调用外部exe文件
- 斐波那契数列的素数个数
- 前端开发知识结构
- PHP学习笔记