AIDL File.
package com.birds.app.arrange;
import java.util.Map;
import java.util.List;
import android.graphics.Bitmap;
interface ImageDownloadService {
int getPID();
Bitmap downloadBitmap(String url);
}
实现;
/**
*
*/
package com.birds.app.arrange;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
/**
* @author birds
*
*/
public class ImageDownloadServiceImpl extends Service {
private MyImageDownload impl = null;
@Override
public void onCreate() {
super.onCreate();
impl = new MyImageDownload();
}
@Override
public IBinder onBind(Intent intent) {
return impl;
}
class MyImageDownload extends ImageDownloadService.Stub {
@Override
public int getPID() throws RemoteException {
return Process.myPid();
}
@Override
public Bitmap downloadBitmap(String url) throws RemoteException {
// your code here
return null;
}
}
}
Activity.
public class MainActivity extends Activity {
private ImageDownloadService impl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private ServiceConnection serviceConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
impl = ImageDownloadService.Stub.asInterface(service);
Log.d("",">>>>Get A Service Instance>>" + impl);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
protected void onResume() {
super.onResume();
bindService(new Intent(this, ImageDownloadServiceImpl.class),
serviceConn, Context.BIND_AUTO_CREATE);
}
@Override
protected void onPause() {
super.onPause();
unbindService(serviceConn);
}
protected void onDestroy() {
super.onDestroy();
}
}