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

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();
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值