android 实现真正意义上的服务及源代码下载
android APP后台服务和长期server长期互动,保证实时数据,这个小项目主要实施app退出后仍然能够执行服务。
使用该系统的Intent.ACTION_TIME_TICK现,这个系统的广播每隔一分钟就进行广播一次,能够在程序中接收该广播消息。接收到之后检測app中的service服务是否在执行,假设在执行。则不处理,假设没有执行,则又一次启动该service服务。
值得注意的是,尽管本演示样例能够实现后台执行服务的功能,可是当用户按home键进行清楚内存的时候依旧能够把app清楚内存。app清楚内存之后,不再执行,当然也就不能实现什么功能(如接收消息推送),就是达不到像QQ、微信那样,清楚内存之后,QQ和微信依旧在后台执行。
以下開始讲述本项目的实现过程。
我们知道。注冊广播接收者有两种方式,一种是在配置文件里进行配置,还有一种是在代码中进行注冊。该广播须要在代码中进行注冊。这里须要注意的是注冊广播接收者能够在Activity中进行注冊,可是假设在Activity中进行注冊。必须在onDestory的时候进行注销广播接收者,假设不注销,会出现Error错误。
有由于希望该广播接收者在app退出之后。依旧能够接收到系统广播消息,所以此处注冊系统的广播接受者应该在Application类中完毕。
代码例如以下:
public class MyApplication extends Application {
private static MyApplication myApplication;
public MyApplication() {
}
public static MyApplication getInstance(){
if (myApplication == null) {
myApplication = new MyApplication();
}
return myApplication;
}
@Override
public void onCreate() {
super.onCreate();
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_TIME_TICK);
MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver, intentFilter);
}
}
在接收到系统广播的消息之后,利用自己定义的广播接受者MyReceiver进行处理,在onReceive方法中检測要启动的Service类是否已经在后台执行,假设在后台执行,则不处理。假设没有在后台执行,则启动该服务。
代码例如以下:
public class MyReceiver extends BroadcastReceiver {
private final String TAG = MyReceiver.class.getSimpleName();
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.i(TAG, "监听到开机启动getAction");
}else if(intent.getAction().equals(Intent.ACTION_TIME_TICK)){
Log.i(TAG, "监听到TIME_TICK");
boolean isServiceRunning = false;
ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service :manager.getRunningServices(Integer.MAX_VALUE)) {
if("com.yin.service.MyService".equals(service.service.getClassName()))
//Service的全类名
{
isServiceRunning = true;
Log.i(TAG, "已经启动");
}
}
if (!isServiceRunning) {
Intent i = new Intent(context, MyService.class);
context.startService(i);
Log.i(TAG, "没有启动,如今启动");
}
}else {
Log.i(TAG, "监听到其它");
}
}
}
以下给出MyService类的代码,该代码并不完毕什么功能
public class MyService extends Service {
private final String TAG = MyService.class.getSimpleName();
public MyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
flags = START_STICKY;
//使用这种标志由于该值会让系统在该service停止之后在进行启动,可是在清除内存之后是无效的
return super.onStartCommand(intent,flags,startId);
}
@Override
public void onCreate() {
super.onCreate();
Notification notification = new Notification();
startForeground(-1, notification);
Log.i(TAG, "Myservice类的oncreate方法");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Intent service = new Intent(this, MyService.class);
startService(service);//这里是一个取巧的方法,在该service销毁是在进行启动。可是清楚该app内存之后依旧不能起作用
}
}
以下给出配置文件代码:
<?xml version="1.0" encoding="utf-8"?
>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yin.servicetest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:persistent="true"
android:name="com.yin.application.MyApplication" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.yin.servicetest.MyReceiver">
</receiver>
<service android:name="com.yin.service.MyService">
</service>
</application>
</manifest>
结果:
值得注意的是。尽管那能够达到后台执行的目的,可是假设在用户清楚内存之后这个方案就不起作用了。假设大家有谁知道QQ或者微信怎样实如今清楚内存之后怎样实现依旧在后台执行进程和服务的。请指教!
【握手】
版权声明:本文博客原创文章,博客,未经同意,不得转载。
android 实现真正意义上的服务及源代码下载的更多相关文章
- WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]
原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...
- Android Fragment真正意义上的onResume和onPause
Fragment虽然有onResume和onPause的,但是这两个方法是Activity的方法,调用时机也是与Activity相同,和ViewPager搭配使用这个方法就很鸡肋了,根本不是你想要的效 ...
- Android Service完全解析,关于服务你所需知道的一切(上)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...
- (转) Android Service完全解析,关于服务你所需知道的一切(上)
相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的Android程序员如果连Service都没听说过的话,那确实也太逊了.Service作为Android四大组件之一,在每一个应用程序 ...
- Android Service完全解析,关于服务你所需知道的一切(上) (转载)
转自:http://blog.csdn.net/guolin_blog/article/details/11952435 转载请注明出处:http://blog.csdn.net/guolin_blo ...
- Android Service完全解析,关于服务你所需知道的一切(下)
转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...
- 【转】Android Service完全解析,关于服务你所需知道的一切(下) ---- 不错
原文网址:http://blog.csdn.net/guolin_blog/article/details/9797169 转载请注册出处:http://blog.csdn.net/guolin_bl ...
- [转]Android Service完全解析,关于服务你所需知道的一切
目录(?)[+] Android Service完全解析,关于服务你所需知道的一切(上) 分类: Android疑难解析2013-10-31 08:10 6451人阅读 评论(39) 收藏 举报 ...
- Android Service完全解析,关于服务你所需知道的一切(下) (转载)
转自:http://blog.csdn.net/guolin_blog/article/details/9797169 转载请注册出处:http://blog.csdn.net/guolin_blog ...
随机推荐
- MongoDB最新版本3.2.9下载地址
https://downloads.mongodb.com/win32/mongodb-win32-x86_64-enterprise-windows-64-3.2.9.zip?_ga=1.22538 ...
- HYSBZ 2243 染色 (树链拆分)
主题链接~~> 做题情绪:这题思路好想.调试代码调试了好久.第一次写线段树区间合并. 解题思路: 树链剖分 + 线段树区间合并 线段树的端点记录左右区间的颜色.颜色数目.合并的时候就用区间合并的 ...
- mac平台adb、tcpdump捕手android移动网络数据包
在移动电话的发展app当我们希望自己的下才能看到app网络发出请求,这个时候我们需要tcpdump工具包捕获.实现tcpdump空灵,以下步骤需要: 在这里,在android 华为手机 P6对于样本 ...
- C++ STL简化了编程
图1.STL和c++标准模板库 作为C++标准必不可少的一部分,STL应该是渗透在C++程序的角角落落里的. STL不是实验室里的宠儿.也不是程序猿桌上的摆设.她的激动人心并不是昙花一现.本教程旨在 ...
- <ASP.NET4 从入门到精通>学习笔记1
非常久没有写东西了,今日有时间,開始整理一下关于ASP.NET 4的学习笔记.提醒自己,也欢迎评论. 概述一共分为6个大的部分,兴许文章.将依据每一个部分进行整理,本读书笔记仅仅是整理关键点,对于啰嗦 ...
- Linux 的 Shell
一个:Shell 概念 shell 这个词是不奇怪,意思是 "壳" 这是间OS 用户和芯层之间的相互作用,在linux系统.用户可以通过命令终端.使用shell 命令向下传达他们的 ...
- 获取鼠标点击相对于Canva位置的2种方法
如果给Canvas添加 onmousedown事件,获取到的鼠标位置都是相对于当前文档的位置(x,y):
- 如何识别SQL Server中的IO瓶颈
原文:如何识别SQL Server中的IO瓶颈 原文出自: http://www.mssqltips.com/sqlservertip/2329/how-to-identify-io-bottlene ...
- Codeforces Round #107 (Div. 2)---A. Soft Drinking
Soft Drinking time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Team Foundation Server 2015使用教程--新增权限为读取器的团队