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. YT新人之巅峰大决战04

    Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Ed ...

  2. HDU 4391 Paint The Wall 段树(水

    意甲冠军: 特定n多头排列.m操作 以下是各点的颜色 以下m一种操纵: 1 l r col 染色 2 l r col 问间隔col色点 == 通的操作+区间内最大最小颜色数的优化,感觉非常不科学... ...

  3. Oracle Applications Multiple Organizations Access Control for Custom Code

    档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...

  4. 开源:矿Android新闻client,快、小、支持离线阅读、操作简单、内容丰富,形式多样展示、的信息量、全功能 等待(离开码邮箱)

    分享:矿Android新闻client.快.小.支持离线阅读.操作简单.内容丰富,形式多样展示.的信息量.全功能 等待(离开码邮箱) 历时30天我为了开发这个新闻clientAPP,下面简称觅闻 ht ...

  5. mysql声明摘要

    前一段时间,和学生参加该项目的最终完成,主要的项目是做一个报告,它涉及到很多sql声明,因此,采取下一个汇总. 一.基金会 1.数据库相关的命令 a>.创建数据库 CREATE DATABASE ...

  6. Struts1.X与Spring集成——第一种方案

    spring+struts(第一种方案) 集成原理:在Action中取得BeanFactory,通过BeanFactory取得业务逻辑对象,调用业务逻辑方法. 一,新建一个项目Spring_Strut ...

  7. C该结构变化 struct typedef

     这几天看代码,看到若干类型的结构,例如下列结构声明: struct    book{ string name; int price; int num; }; 此种结构定义结构变量的格式例如以下: ...

  8. 兔子--Spring基金会

    设计模式的基本目的: 对象之间的解耦.使用容器来管理组件.减少不同组件之间的耦合 控制返回,搜索请求委托给容器 将积极考虑被动接受 版权声明:本文博主原创文章,博客,未经同意不得转载.

  9. 【ASP.NET】判断访问网站的客户端是PC还是手机

    原文:[ASP.NET]判断访问网站的客户端是PC还是手机 主要就是通过客户端传递的User-agent来判断访问网站的客户端是PC还是手机,.NET中就是Request.ServerVariable ...

  10. Duanxx的Altium Designer学习:PCB试想一下,在目前的水平

    1 Shift+S          这个快捷键能高亮当前层,而且使其它层变成灰色.见下图: 2 隐藏指定层 在图中右下角的地方,右键.会弹出一个选项条.选择Hide Layers.能够选择想要隐藏的 ...