Android通过AIDL实现下载进程通信
2012-11-08 19:04:01 来源:WEB开发网import com.obatu.services.aidl.IDownLoadService;
import com.obatu.services.aidl.Queue;
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.Environment;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class DownLoadActivity extends Activity {
private static final String SERVICE_ACTION = "com.obatu.service.download_service";
//服务接口
private IDownLoadService downloadService;
//服务绑定器
private ServiceConnection sConnect = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("ServiceConnection", "onServiceDisconnected() called");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 绑定到服务
Log.i("ServiceConnection", "onServiceConnected() called");
downloadService = IDownLoadService.Stub.asInterface(service);
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//绑定服务
Intent intent = new Intent(SERVICE_ACTION);
bindService(intent, sConnect, Context.BIND_AUTO_CREATE);
Button startdown = (Button)findViewById(R.id.startdown);
//下载操作
startdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Queue queue = new Queue();
queue.setId(1);
queue.setName("愤怒的小鸟");
queue.setSavePath(Environment.getExternalStorageDirectory().getAbsolutePath()+"/angrybirds.apk");
queue.setUrl("http://www.obatu.com/download/app?code=47c10b353d6892d5d50bef4cfc788436");
//queue.setDowningIntent("com.obatu.client.DownLoadActivity");
//queue.setCompleteIntent("com.obatu.client.installActivity");
try {
downloadService.down(queue);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
更多精彩
赞助商链接