A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding <service> declaration in its package's AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().
package com.example.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return new CatImpl();
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
第二步:創(chuàng)建AIDL(在包名--右鍵--aidl文件---名字---finish)
// ICat.aidl
package com.example.servicedemo;
// Declare any non-default types here with import statements
interface ICat {
void setName(String name);
String desc();
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
第三步:創(chuàng)建一個(gè)接口用來實(shí)現(xiàn)aidl
package com.example.servicedemo;
import android.os.RemoteException;
import android.provider.MediaStore;
/**
* Created by 若蘭 on 2016/2/13.
* 一個(gè)懂得了編程樂趣的小白,希望自己
* 能夠在這個(gè)道路上走的很遠(yuǎn),也希望自己學(xué)習(xí)到的
* 知識(shí)可以幫助更多的人,分享就是學(xué)習(xí)的一種樂趣
* QQ:1069584784
* csdn:http://blog.csdn.net/wuyinlei
*/
public class CatImpl extends ICat.Stub {
private String name;
@Override
public void setName(String name) throws RemoteException {
this.name = name;
}
@Override
public String desc() throws RemoteException {
return "hello , my name is " + name;
}
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
}