我正在实施一张卡片交换申请。我想通过单击按钮通过Intent将数据发送到同一个活动。我发现由这些重写方法startActivityForResult
和onResult管理的Intent存在问题,来自https://coderwall.com/p/bewxvw/starting-an-activity-with-result-the-clean-way,这是适合我使用的实现。当我使用OnNewIntent()
获取时,我的意图为空。
Tx为您提供了很多帮助
package org.key.plugin.android;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class AndroidReader extends BaseActivity {
private static final String TAG = "AndroidReader";
public static final String MIME_TEXT_PLAIN = "text/plain";
private NfcAdapter mNfcAdapter;
byte[] resp = {};
private Button button = null;
/*****
* Manage NFC
*
**********/
/*
* (non-Javadoc)
*
* @see android.app.Service#onCreate()
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize your service here
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
// finish();
return;
}
if (!mNfcAdapter.isEnabled()) {
// mTextView.setText("NFC is disabled.");
} else {
// mTextView.setText(R.string.explanation);
}
resolveIntent(getIntent());
button = (Button)findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Code here executes on main thread after user presses button
Log.i(TAG, "context :"+v.getContext());
Intent intent = new Intent(v.getContext(), AndroidReader.class);
byte[] data = {0x00, (byte)0xA4, 0x04, 0x00, 0x10, (byte)0xA0, 0x00, 0x00, 0x04, 0x04};
intent.putExtra("req", data);
startActivityForResult(intent, Activity.RESULT_OK, new SuccessResultHandler() {
@Override
public void onResult(Intent onResultIntent) {
//here you have your results data. The resultCode has already been checked against Activity.RESULT_OK
resp = onResultIntent.getByteArrayExtra("resp");
Log.i(TAG, "onResult"+resp.toString());
}
});
// transmit(null);
}
});
}
@Override
protected void onResume() {
super.onResume();
/**
* It's important, that the activity is in the foreground (resumed). Otherwise
* an IllegalStateException is thrown.
*/
setupForegroundDispatch(this, mNfcAdapter);
// mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
// mTechLists);
}
@Override
protected void onPause() {
/**
* Call this before onPause, otherwise an IllegalArgumentException is thrown as well.
*/
stopForegroundDispatch(this, mNfcAdapter);
super.onPause();
// mNfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onNewIntent(Intent intent) {
/**
* This method gets called, when a new Intent gets associated with the current activity instance.
* Instead of creating a new activity, onNewIntent will be called. For more information have a look
* at the documentation.
*
* In our case this method gets called, when the user attaches a Tag to the device.
*/
resolveIntent(intent);
}
private void resolveIntent(Intent intent) {
Log.i(TAG, "Etape 0");
String action = intent.getAction();
Log.i(TAG, "Action "+action);
// ok for Intent ACTION_TAG_DISCOVERED, "action : ACTION_TAG_DISCOVERED"
// nok when i click on the button, "action : null"
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
// status_Data.setText("Discovered tag with intent: " + intent);
Log.i(TAG, "Etape 1");
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
IsoDep myTag = IsoDep.get(tagFromIntent);
Log.i(TAG, "Etape 2");
try {
Log.i(TAG, "Etape 3");
myTag.connect();
// status_Data.setText("Portable de contrôle");
Log.i(TAG, "Etape 4");
if (myTag.isConnected()) {
byte[] data1 = {0x00, (byte)0xA4, 0x04, 0x00, 0x10, (byte)0xA0, 0x00, 0x00, 0x04};
byte[] responseAPDU = myTag.transceive(data1);
}
myTag.close();
} catch (IOException e) {
// Log.e(TAG, e.getLocalizedMessage());
// showAlert(NETWORK);
}
}else if("org.key.plugin.android".equals(action)) {
// status_Data.setText("Discovered tag with intent: " + intent);
Log.i(TAG, "Key Etape 1");
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
IsoDep myTag = IsoDep.get(tagFromIntent);
Log.i(TAG, "Key Etape 2");
try {
Log.i(TAG, "Key Etape 3");
myTag.connect();
// status_Data.setText("Portable de contrôle");
Log.i(TAG, "Key Etape 4");
if (myTag.isConnected()) {
byte[] data = {};
//byte[] data = {0x00, 0x01};
//byte[] data = {0x00, (byte)0xA4, 0x04, 0x00, 0x10, (byte)0xA0, 0x00, 0x00, 0x04, 0x04, 0x01, 0x25, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//return Convert.sHexToByteArray(cmd);
//byte[] data = intent.getExtras("string");
Bundle extras = getIntent().getExtras();
if(extras == null) {
data= null;
} else {
data= extras.getByteArray("req");
}
byte[] responseAPDU = myTag.transceive(data);
Intent returnIntent= new Intent(AndroidReader.this, AndroidReader.class);
Log.i(TAG, byteArrayToSHex(responseAPDU));
returnIntent.putExtra("resp",responseAPDU);
///have a code for each activity that you could resume to
setResult(Activity.RESULT_OK, returnIntent);
finish();
}else
{
Log.i(TAG, "Key Etape 5, deconnect");
}
myTag.close();
} catch (IOException e) {
// Log.e(TAG, e.getLocalizedMessage());
// showAlert(NETWORK);
}
}
}
/**
* @param activity The corresponding {@link Activity} requesting the foreground dispatch.
* @param adapter The {@link NfcAdapter} used for the foreground dispatch.
*/
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
adapter.enableForegroundDispatch(activity, pendingIntent, null, null);
}
/**
* @param activity The corresponding {@link Activity} requesting to stop the foreground dispatch.
* @param adapter The {@link NfcAdapter} used for the foreground dispatch.
*/
public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
adapter.disableForegroundDispatch(activity);
}
}