1. Service 服务 (是一个没有用户界面的在后台运行执行耗时操作的应用组件)
第一种方式: (startService 未绑定, 当程序退出,若没有停止服务则会继续在后台运行)
//继承 Servicepublic class MyService extends Service {private String tag = "MyService";@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();Log.i(tag , "onCreate");}}<!-- 清单中声明 --><service android:name=".MyService"></service>
//开启服务intent = new Intent(this, MyService.class);startService(intent);
//停止服务stopService(intent);
第二种方式: (绑定服务)
//继承 Servicepublic class MyService extends Service {private String tag = "MyService";@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn new MyBind();}public class MyBind extends Binder implements IServices {@Overridepublic void test() {myTest();}}private void myTest() {Log.i(tag, "test in service");}}//binder 中的接口public interface IServices {void test();}//开启服务intent = new Intent(this, MyService.class);bindService(intent, conn , Context.BIND_AUTO_CREATE);mIsBound = true;if (mIsBound) {unbindService(conn);}private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {iServices = (IServices)service;Log.i(tag, "onServiceConnected");}};//调用服务中的函数public void onTest(View view) {iServices.test();}
远程调用Service
调用其他程序提供的Service, 其用到了aidl技术. 其他和调用本地无异. 只需将interface 的 public 修饰符去掉即可, 后缀改为aidl.
编译器将自动在gen目录下生成相应的.java 文件, 其中的Stub 子类实现了接口.
服务端://aidl 接口定义interface IServices {void test();}public class RemoteService extends Service {private String tag = "RemoteService";@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn new MyBinder();}private class MyBinder extends IServices.Stub{@Overridepublic void test() throws RemoteException {myTest();}}private void myTest() {Log.i(tag, "test in service");}}
<!-- 清单注册,并添加action --><service android:name=".RemoteService"><intent-filter ><action android:name="cn.lt.RemoteService"/></intent-filter></service>
在调用端我们需要添加其服务端定义的aidl到工程中,其包名需要和服务端的一致.
private IServices iServices;//绑定Intent intent = new Intent();intent.setAction("cn.lt.RemoteService");bindService(intent, new MyConn(), BIND_AUTO_CREATE);private class MyConn implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {iServices = IServices.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {}}//调用服务中的函数iServices.test();
2. Java 反射机制 (是一个没有用户界面的在后台运行执行耗时操作的应用组件)
Class 类
Class 类为 Java 反射的源头, 通过该类能得到一个类的完整结构. (在android 开发中, 可通过反射机制得到系统中隐藏的服务.)//反射测试类public class Person {public Person(){};public static void sayHello() {System.out.println("Hello World");}public void sayHello(String name) {System.out.println("Hello "+name);}public int add(int a, int b) {return a+b;}}//---------------------------------public static void main(String[] args) {try {//得到 Person 类对象Class c = Class.forName("cn.lt.Test.Person");//得到 sayHello 方法Method method1 = c.getMethod("sayHello");method1.invoke(null); //调用静态方法可传nullMethod method2 = c.getMethod("sayHello", String.class);method2.invoke(c.newInstance(), new Object[]{"Ty"});Method method3 = c.getMethod("add", int.class, int.class);System.out.println(method3.invoke(c.newInstance(), new Object[]{1, 2}));} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}