在android编写Service入门中介绍了android的两种后台服务,本地和远程的。这里用本地服务做了一个模拟定时后台发短信的技术原型。

启动该示例应用的样子:

选择启动:

启动后,可看到ddms logcat日志:

这是操作:启动、停止和退出应用,显示的调试日志信息。模拟后台服务每5秒钟发送一次短信信息。

主要代码,后台服务SmsService:

package com.easymorse;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.util.Log;

public class SmsService extends Service {

private boolean started;

private boolean threadDisable;

private ServiceBinder serviceBinder = new ServiceBinder();

public class ServiceBinder extends Binder implements ISmsService {

@Override

public boolean isStarted() {

return started;

}

@Override

public void start() {

started=true;

Log.d(“sms.service”, “sms service started.”);

}

@Override

public void stop() {

started=false;

Log.d(“sms.service”, “sms service stopped.”);

}

}

@Override

public IBinder onBind(Intent intent) {

return serviceBinder;

}

@Override

public void onCreate() {

super.onCreate();

Thread thread = new Thread() {

@Override

public void run() {

while (!threadDisable) {

try {

if (started) {

Log.d(“sms.service”, “send a sms message.”);

}

Thread.sleep(1000 * 5);

} catch (InterruptedException e) {

}

}

}

};

thread.start();

Log.d(“sms.service”, “sms service created.”);

}

@Override

public void onDestroy() {

super.onDestroy();

threadDisable = true;

Log.d(“sms.service”, “sms service shutdown.”);

}

}

前台的Actvity代码,SmsServiceOptions:

package com.easymorse;

import android.app.TabActivity;

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.IBinder;

import android.util.Log;

import android.widget.RadioGroup;

import android.widget.TabHost;

import android.widget.RadioGroup.OnCheckedChangeListener;

public class SmsServiceOptions extends TabActivity {

private RadioGroup radioGroup;

private ISmsService smsService; private ServiceConnection serviceConnection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

smsService = (ISmsService) service;

if (smsService.isStarted()) {

radioGroup.check(R.id.radioButtonStart);

} else {

radioGroup.check(R.id.radioButtonStop);

}

radioGroup

.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group,

int checkedId) {

if (checkedId == R.id.radioButtonStart) {

Log.d(“sms.service”, “starting service…”);

smsService.start();

} else {

Log.d(“sms.service”, “stopping service…”);

smsService.stop();

}

}

});

}

@Override

public void onServiceDisconnected(ComponentName name) {

smsService = null;

}

};

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

this.setTitle(“短信服务器”);

this.bindService(new Intent(“com.easymorse.SmsService”),

this.serviceConnection, BIND_AUTO_CREATE);

TabHost tabHost = this.getTabHost();

tabHost.setup();

TabHost.TabSpec spec = tabHost.newTabSpec(“服务选项”);

spec.setContent(R.id.Option01);

spec.setIndicator(“服务选项”);

tabHost.addTab(spec);

spec = tabHost.newTabSpec(“服务状态”);

spec.setContent(R.id.Option02);

spec.setIndicator(“服务状态”);

tabHost.addTab(spec);

radioGroup = (RadioGroup) this.findViewById(R.id.radioGroup01);

}

@Override

protected void onDestroy() {

super.onDestroy();

this.unbindService(serviceConnection);

}

}

另外,不要忘记在配置文件中增加对service的声明,见AndroidManafest.xml:

<?xml version=”1.0″ encoding=”utf-8″?>

package=”com.easymorse” android:versionCode=”1″ android:versionName=”1.0″>

源代码见:

http://easymorse.googlecode.com/svn/tags/android.local.service.1.demo/

这个示例的局限性在于,如果Activity退出,则后台服务也一起退出。本地服务是否可以在Activity退出后依然在后台执行,还需要进一步了解android api。我猜测应该是不可以的,本地服务和Activity可能是同一个进程的不同线程关系。

更多相关文章

  1. android进程间服务通信
  2. Android学习-----如何使用sqlite进行后台数据交互,sqlite入门使用
  3. Android(安卓)辅助功能(无障碍)自定义开发类似微信抢红包插件
  4. 在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬
  5. 基于Android的校园快递系统_移动端设计_后台管理_MySQL数据库设
  6. Android(安卓)使用LeakCanary 检测内存泄露
  7. Android(安卓)调用.Net WCF服务
  8. Android与服务器端数据交互(http协议整合struts2+android)
  9. Android客户端应用享用传统Web服务

随机推荐

  1. Wireshark分析艺术【读书总结】
  2. OpenTelemetry 简析
  3. 16 个好用的 Code Review 工具
  4. Kubernetes 稳定性保障手册 -- 可观测性
  5. 《我与Windows Server 2008R2那点事儿》
  6. Istio Proxy【Envoy扩展】详解
  7. MySql引擎原理以及优化
  8. js的解构
  9. VARCHART XGantt甘特图如何Drag & Drop
  10. 说说心里话