概述
服务客户端(MainActivity):
package com.example.remoteservicebind;
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.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Process;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "BindRemoteService";
private IRemoteService mRemoteService = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bindRemoteService = (Button) findViewById(R.id.bind_remote_service);
bindRemoteService.setOnClickListener(mBindListener);
Button unBindRemoteService = (Button) findViewById(R.id.unbind_remote_service);
unBindRemoteService.setOnClickListener(mUnBindListener);
Button remoteServiceMethod = (Button) findViewById(R.id.remote_service_method);
remoteServiceMethod.setOnClickListener(mRemoteServiceMethodListener);
Button killRemoteServiceProcess = (Button) findViewById(R.id.kill_remote_service_process);
killRemoteServiceProcess.setOnClickListener(mKillRemoteServiceProcessListener);
}
private final OnClickListener mBindListener = new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "Main Activity ----> onClick BindRemoteService");
bindService(new Intent(IRemoteService.class.getName()), mRemoteServiceConnection, Context.BIND_AUTO_CREATE);
}
};
private final OnClickListener mUnBindListener = new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "Main Activity ----> onClick unBindService");
unbindService(mRemoteServiceConnection);
mRemoteService = null;
}
};
private final OnClickListener mRemoteServiceMethodListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (mRemoteService == null) {
Toast.makeText(MainActivity.this, "没有绑定到服务", Toast.LENGTH_LONG).show();
} else {
try {
Toast.makeText(MainActivity.this, mRemoteService.getName(), Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
}
}
}
};
private final OnClickListener mKillRemoteServiceProcessListener = new OnClickListener() {
@Override
public void onClick(View v) {
int pid;
try {
pid = mRemoteService.getPid();
Process.killProcess(pid);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
private final ServiceConnection mRemoteServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mRemoteService = IRemoteService.Stub.asInterface(service);
}
};
private final Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
远程服务(RemoteService):
package com.example.remoteservicebind;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.util.Log;
/**
* 远程服务 开启在另外一个进程中
* @author Simon Xu
*
* 2013-4-11上午11:12:44
*/
public class RemoteService extends Service {
private static final String TAG = "BindRemoteService";
private static final int REPORT_MSG = 1;
// private final Handler mHandler = new Handler(){
//
// @Override
// public void handleMessage(Message msg) {
//
// }
//
// };
private final IRemoteService.Stub mRemoteServiceBinder = new IRemoteService.Stub() {
@Override
public int getPid(){
Log.i(TAG, "Remote Service ----> IRemoteService & getPid()");
return Process.myPid();
}
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString)
throws RemoteException {
}
@Override
public String getName(){
Log.i(TAG, "Remote Service ----> IRemoteService & getName()");
return "RemoteService diaomao......";
}
};
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "Remote Service ----> onBind");
if (IRemoteService.class.getName().equals(intent.getAction())) {
return mRemoteServiceBinder;
} else {
return null;
}
}
@Override
public void onCreate() {
Log.i(TAG, "Remote Service ----> onCreate");
// this.mHandler.sendEmptyMessage(REPORT_MSG);
}
@Override
public void onDestroy() {
Log.i(TAG, "Remote Service ----> onDestroy");
// this.mHandler.removeMessages(REPORT_MSG);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Remote Service ----> onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
}
IRemoteService.aidl:
package com.example.remoteservicebind;
interface IRemoteService{
int getPid();
String getName();
void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString);
}
最后
以上就是矮小书本为你收集整理的android 开启远程服务,Android绑定远程Service(此Service开启在另外一个进程中)的全部内容,希望文章能够帮你解决android 开启远程服务,Android绑定远程Service(此Service开启在另外一个进程中)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复