aidl文件
- package com.tang.remoteservicedemo;
- interface IRemoteService {
- String getInfo();
- }
实现
package com.tang.remoteservicedemo;
import java.util.Date;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class IService extends Service
{
private IBinder iBinder = new IRemoteService.Stub() {
@Override
public String getInfo() throws RemoteException {
// TODO Auto-generated method stub
return new Date(System.currentTimeMillis()).toLocaleString()+" 来自远程服务的信息!";
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return iBinder;
}
}
import java.util.Date;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class IService extends Service
{
private IBinder iBinder = new IRemoteService.Stub() {
@Override
public String getInfo() throws RemoteException {
// TODO Auto-generated method stub
return new Date(System.currentTimeMillis()).toLocaleString()+" 来自远程服务的信息!";
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return iBinder;
}
}
应用实现
- <service android:name="com.tang.remoteservicedemo.IService"
- android:process=":remote">
- <intent-filter>
- <action android:name="com.tang.remoteservicedemo.IService"/>
- </intent-filter>
- </service>
实现:
- package com.tang.clientdemo;
- import java.util.Timer;
- import java.util.TimerTask;
- import com.tang.remoteservicedemo.IRemoteService;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.util.Log;
- public class MainActivity extends Activity {
- private IRemoteService iService = null;
- private boolean isBinded =false;
- ServiceConnection conn = new ServiceConnection() {
- @Override
- public void onServiceDisconnected(ComponentName name) {
- // TODO Auto-generated method stub
- isBinded = false;
- iService = null;
- }
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- // TODO Auto-generated method stub
- iService = IRemoteService.Stub.asInterface(service);
- isBinded = true;
- }
- };
- public void doBind()
- {
- Intent intent = new Intent("com.tang.remoteservicedemo.IService");
- bindService(intent, conn, Context.BIND_AUTO_CREATE);
- }
- public void doUnbind()
- {
- if (isBinded)
- {
- unbindService(conn);
- iService = null;
- isBinded = false;
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- new Thread(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- doBind();
- }
- }).start();
- Timer timer = new Timer();
- timer.schedule(task, 0, 2000);
- }
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- if(iService!=null)
- {
- try {
- Log.i("AAA", iService.getInfo());
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- else
- {
- Log.i("AAA", "iService!=null");
- }
- }
- };
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- doUnbind();
- task.cancel();
- super.onDestroy();
- }
- }