aidl 实现远程服务调用 及 回调

来源:互联网 发布:php post 实现 get 编辑:程序博客网 时间:2024/06/01 09:37
package com.junnet.heepay.service;


import java.util.Map;


import com.junnet.heepay.activity.LoginActivity;
import com.junnet.heepay.util.PayInfo;


import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.util.Log;


public class HeepayService extends Service {
private static  String TAG = "HeepayService";
private static  RemoteCallbackList<IRemoteServiceCallback> callbacks = new RemoteCallbackList<IRemoteServiceCallback>();
public final IRemoteHeepayService.Stub mBinder = new IRemoteHeepayService.Stub() {
    
// 去支付
@Override
public String goToPay(Bundle payInfo) throws RemoteException {
PayInfo.payinfo = payInfo;
// 首先得登录
System.out.println("goToPay");
Intent intent = new Intent();
intent.setAction("com.junnet.heepay.activity.LoginActivity");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(getApplicationContext(), LoginActivity.class);
// intent.putExtra("payinfo", bundle);
HeepayService.this.startActivity(intent);
            
// callbacks.getBroadcastItem(0).startActivity("com.junnet.business",
// "com.junnet.business.MainActivity", returnBundle());
// Log.e(TAG, "startActivity");
return "成功";
}


@Override
public void registerCallback(IRemoteServiceCallback callback)
throws RemoteException {
callbacks.register(callback);


}


@Override
public void unregisterCallback(IRemoteServiceCallback callback)
throws RemoteException {
callbacks.unregister(callback);


}


};


@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "success in onBind");
return mBinder;
}


public void onCreate() {


printf("service on create");
super.onCreate();


};


@Override
public void onDestroy() {
printf("service on destroy");
super.onDestroy();
}


@Override
public boolean onUnbind(Intent intent) {
printf("service on unbind");
return super.onUnbind(intent);
}


public void onRebind(Intent intent) {
printf("service on rebind");
super.onRebind(intent);
}


private void printf(String str) {
Log.v(TAG, "###################------ " + str + "------");
}


private Bundle returnBundle() {
Bundle bundle = new Bundle();
bundle.putString("bundle", "bundle");
return bundle;
}
       
// 回调方法
public static void callBack(String packageName, String activityName, Bundle data) {
int n = callbacks.beginBroadcast();
Log.i(TAG, "mCallbacks.beginBroadcast() is " + n);
try {
for (int i = 0; i < n; i++) {
System.out.println("~~callBack~~");
callbacks.getBroadcastItem(i).startActivity(packageName,
activityName, data);
callbacks.unregister(callbacks.getBroadcastItem(i));
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
callbacks.finishBroadcast();
}


}
原创粉丝点击