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 实现真正意义上的服务及源代码下载的更多相关文章

  1. WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]

    原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...

  2. Android Fragment真正意义上的onResume和onPause

    Fragment虽然有onResume和onPause的,但是这两个方法是Activity的方法,调用时机也是与Activity相同,和ViewPager搭配使用这个方法就很鸡肋了,根本不是你想要的效 ...

  3. Android Service完全解析,关于服务你所需知道的一切(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...

  4. (转) Android Service完全解析,关于服务你所需知道的一切(上)

    相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的Android程序员如果连Service都没听说过的话,那确实也太逊了.Service作为Android四大组件之一,在每一个应用程序 ...

  5. Android Service完全解析,关于服务你所需知道的一切(上) (转载)

    转自:http://blog.csdn.net/guolin_blog/article/details/11952435 转载请注明出处:http://blog.csdn.net/guolin_blo ...

  6. Android Service完全解析,关于服务你所需知道的一切(下)

    转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...

  7. 【转】Android Service完全解析,关于服务你所需知道的一切(下) ---- 不错

    原文网址:http://blog.csdn.net/guolin_blog/article/details/9797169 转载请注册出处:http://blog.csdn.net/guolin_bl ...

  8. [转]Android Service完全解析,关于服务你所需知道的一切

      目录(?)[+] Android Service完全解析,关于服务你所需知道的一切(上) 分类: Android疑难解析2013-10-31 08:10 6451人阅读 评论(39) 收藏 举报 ...

  9. Android Service完全解析,关于服务你所需知道的一切(下) (转载)

    转自:http://blog.csdn.net/guolin_blog/article/details/9797169 转载请注册出处:http://blog.csdn.net/guolin_blog ...

随机推荐

  1. 改动symbol link的owner

    当/home/jenkins文件夹空间不足的时候,能够先查看哪个文件夹在较大的磁盘分区上,然后将jenkins文件夹移动过去 最后创建/home/jenkins link到新位置. 这时候须要改动sy ...

  2. OWIN 为WebAPI

    OWIN 为WebAPI 宿主 跨平台 OWIN是什么? OWIN的英文全称是Open Web Interface for .NET. 如果仅从名称上解析,可以得出这样的信息:OWIN是针对.NET平 ...

  3. Directx11学习笔记【十五】 基本几何体的绘制

    本文由zhangbaochong原创,转载请注明出处http://www.cnblogs.com/zhangbaochong/p/5573970.html 前面实现简单地形的教程,我们只是绘制了一个网 ...

  4. lunux命令笔记

    文件查看命令 ls / -lh ls list / 路径 -l 具体 -lh 具体的人性化显示 -ld 显示文件夹 -i 显示i节点 mkdir /tmp/mulu/mulu2 /tmp/ma/mb ...

  5. PHP情人:p十几天来学习hp第一天

    我这里是暂时的 Apache web server 和 MY SQL 如WEB,在php-4.3.3下的环境做的程序.当然要简单的构建和訪问查看数据库 PHPMYADMIN 不可少.  以下简介一下P ...

  6. Lua Development Tools (LDT)

    http://www.eclipse.org/ldt/ Lua Development Tools (LDT) is about providing Lua developers with an ID ...

  7. springMVC项目异步处理请求的错误Async support must be enabled on a servlet and for all filters involved in async

    从github上down下来一个项目,springMVC-chat.作者全是用的注解,也就是零配置.这可苦了我,经过千辛万苦,终于集成到如今的项目中有一点样子了,结果报出来以下的错误.红色部分.解决方 ...

  8. 虚拟机VM10连衣裙Mac OS X 10.9.3

    最近WWDC终极大招释放--新的编程语言Swift(迅速),导致大波浪,渴望围观程序猿.当然,工欲善其事,其利润,因此,对于那些谁不Mac非常为难.可是,请放心.本文教你怎样在Windows下也能体验 ...

  9. CI-持续集成(2)-软件工业“流水线”技术实现(转)

    1   概述 持续集成(Continuous Integration)是一种软件开发实践.在本系列文章的前一章节已经对其背景及理论体系进行了介绍.本小节则承接前面提出的理论构想进行具体的技术实现. & ...

  10. UVa11488-Hyper Prefix Sets(trie树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...