您的位置:首页 > 移动开发 > Android开发

android 实现真正意义上的服务及源代码下载

2015-08-17 15:01 671 查看
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或者微信怎样实如今清楚内存之后怎样实现依旧在后台执行进程和服务的。请指教!

【握手】

源代码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: