在Intent中的Onclick监听器

时间:2015-03-09 13:11:37

标签: android android-intent nfc onclicklistener

嘿伙计们我是java / android编程的新手,我有一个问题。

是否可以在意图中放置一个onclick按钮,因为我想在每次按下按钮时执行操作。 (单击开始尝试部分)

我要添加我的代码,以便你可以查看它。

非常感谢。

public class transceive extends Activity {
private NfcAdapter mAdapter;
private PendingIntent pendingIntent;

private static final String TAG = "MyActivity";

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.transceive);
    mAdapter = NfcAdapter.getDefaultAdapter(this);
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    //     Button button = (Button) findViewById(R.id.button);
    //    button.setOnClickListener((View.OnClickListener) this);
}

private TextView textout;



public void onNewIntent(Intent intent) {

    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    NfcA nfcA = NfcA.get(tagFromIntent);


    try {
        nfcA.connect();

        byte[] clear = nfcA.transceive(new byte[]{(byte) 0xB4, 0x1F});
        byte[] wreg = nfcA.transceive(new byte[]{(byte) 0xB6, 0x03, 0x04});
        byte[] write = nfcA.transceive(new byte[]{(byte) 0xB1, 0x01});
        byte[] reg1 = nfcA.transceive(new byte[]{(byte) 0xB5, 0x01});
        byte[] clear2 = nfcA.transceive(new byte[]{(byte) 0xB4, 0x1F});
        byte[] read = nfcA.transceive(new byte[]{(byte) 0xB2, 0x00});
        byte[] reg2 = nfcA.transceive(new byte[]{(byte) 0xB5, 0x0D});
        byte[] clear3 = nfcA.transceive(new byte[]{(byte) 0xB4, 0x1F});
        byte[] transceive = nfcA.transceive(new byte[]{(byte) 0xB3, 0x02});
        byte[] reg3 = nfcA.transceive(new byte[]{(byte) 0xB5, 0x01});
        byte[] clear4 = nfcA.transceive(new byte[]{(byte) 0xB4, 0x1F});
        Log.i("NFC", getHexString(clear));
        Log.i("NFC", getHexString(wreg));
        Log.i("NFC", getHexString(read));
        Log.i("NFC", getHexString(reg1));
        Log.i("NFC", getHexString(clear2));
        Log.i("NFC", getHexString(write));
        Log.i("NFC", getHexString(reg2));
        Log.i("NFC", getHexString(clear3));
        Log.i("NFC", getHexString(transceive));
        Log.i("NFC", getHexString(reg3));
        Log.i("NFC", getHexString(clear4));

        textout = (TextView) findViewById(R.id.txtdaten);
        textout.setText(getHexString(transceive));

        nfcA.close();

    } catch (Exception e) {
        Log.e("NFC", e.toString());
    }
    Log.i("NFC", "receiving-&-sending-done");
}


public void onPause() {
    super.onPause();
    mAdapter.disableForegroundDispatch(this);
}

public void onResume() {
    super.onResume();
    mAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}

private String getHexString(byte[] b) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < b.length; i++) {
        if (i > 0)
            sb.append(':');
        sb.append(Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1));
    }
    return sb.toString();
}

}

0 个答案:

没有答案