1. Service 服务 (是一个没有用户界面的在后台运行执行耗时操作的应用组件)
第一种方式: (startService 未绑定, 当程序退出,若没有停止服务则会继续在后台运行)
//继承 Service
public class MyService extends Service {
private String tag = "MyService";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i(tag , "onCreate");
}
}
<!-- 清单中声明 -->
<service android:name=".MyService"></service>
//开启服务
intent = new Intent(this, MyService.class);
startService(intent);
//停止服务
stopService(intent); |
第二种方式: (绑定服务)
//继承 Service
public class MyService extends Service {
private String tag = "MyService";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBind();
}
public class MyBind extends Binder implements IServices {
@Override
public 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() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public 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";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBinder();
}
private class MyBinder extends IServices.Stub{
@Override
public 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 {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iServices = IServices.Stub.asInterface(service);
}
@Override
public 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); //调用静态方法可传null
Method 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 block
e.printStackTrace();
}
}
|