如何绑定活动hostapduservice

时间:2014-07-21 15:47:00

标签: java android android-service android-service-binding

我想在活动和HostApduService之间建立沟通。我没有成功在两者之间进行双向通信,OnBind HostApduService被声明为final,我们是否仅限于使用通知进行通信。到目前为止,我只是成功建立了从HostApduService到Activity的通信。

我的活动课

public class MainActivity extends Activity {

 private static final String TAG = MainActivity.class.getSimpleName();

 private BroadcastReceiver receiver = new BroadcastReceiver() {

     @Override
     public void onReceive(Context context, Intent intent) {
         Bundle bundle = intent.getExtras();
         if (bundle != null) {
             boolean transactionFinished = bundle.getBoolean(HosService.COMMUNICATION_FINISHED);
             String hceServiceMessage = bundle.getString(HostService.APDU_LOG);

             Toast.makeText(MainActivity.this,
                     transactionFinished ? "HCE Transaction accomplished!" : "HCE Transaction in progress!",
                     Toast.LENGTH_LONG).show();

             TextView t = (TextView) MainActivity.this.findViewById(R.id.textView2);
             t.setText(hceServiceMessage);
             t.refreshDrawableState();
       }
     }

    };

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

     }

     @Override
     protected void onResume() {
         super.onResume();
         registerReceiver(receiver, new IntentFilter(HostService.NOTIFICATION));
     }
     @Override
     protected void onPause() {
         super.onPause();
         unregisterReceiver(receiver);
     }

     @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;
     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();

         return super.onOptionsItemSelected(item);
     }

我的服务类:

public class HostService extends HostApduService {

private static final String TAG = HostService.class.getSimpleName();
private static boolean secureSessionInProgress = false;
private static List<String> secureSessionApdu = new ArrayList<String>();
private static String transactionLog = "";
public static final String NOTIFICATION = "net.hce";
public static final String COMMUNICATION_FINISHED = "finished";
public static final String APDU_LOG = "apdu";

public HostService() {
}

@Override
public byte[] processCommandApdu(byte[] cmd_bytes, Bundle bundle) {
    byte[] resp_bytes = null;

   // ***********************************
   // Here I want to exchange data with activity
   // Intent intent = new Intent(NOTIFICATION);
   // intent.putExtra (APDU_LOG, finishedTransactionLog);
   // intent.putExtra(COMMUNICATION_FINISHED, communicationFinished);
   // sendBroadcast(intent);
   // ...
   // How to get info from activity??
   // ************************************ 
   transactionLog += "> ";return resp_bytes;
}

@Override
public void onDeactivated(int reason) {
    secureSessionInProgress = false;
    Log.d(TAG, "deactivated. reason=" + reason);

    transactionLog = "";
}

}

0 个答案:

没有答案