我可以在活动中阅读NFC标签而无需重启吗?

时间:2015-01-21 09:20:32

标签: android android-camera nfc

我正在写一个相机应用程序,当手机读取NFC标签时,它会拍照片

我使用这个例子

https://github.com/josnidhin/Android-Camera-Example

然后修改它

1在CamTestActivity中添加两个属性

PendingIntent pendingIntent;
Tag tag;

2在onCreate

中添加此内容
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

3将此添加到onPause

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);

4 onResume

Log.v("new intent","resume");
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);

5然后添加一个新方法

@Override
protected void onNewIntent(Intent intent) {

    Log.v("new intent","new intent");
    //preview.performClick();
}

但它不起作用

当它读取NFC标签时,它将调用暂停,新意图,恢复。它关闭活动并重新开始,但这次它运行在NewIntent而不是new onCreate

我尝试了很多旗帜,但没有人可以将活动保持在前景

pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK), 0);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT), 0);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FILL_IN_ACTION), 0);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK), 0);

我在Play商店找到了一个应用程序,它被称为" NFC Camera",它可以在没有重启活动的情况下读取NFC标签,该怎么做?

1 个答案:

答案 0 :(得分:4)

在Nfc中当新的Intent被触发时,它将转到onPause()方法然后它转到onNewIntent()menthod。当它转到onNewIntent()时,你会得到标签。当你得到标签时,你必须调用新的Intent()来捕获摄像机图像:

private static final int CAMERA_REQUEST = 1888;
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST);

在该活动中,此处的数据将会出现:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
} 

首先,你必须获得nfc的AndroidMenifest.xml文件的权限。权限是:

 <uses-permission android:name="android.permission.NFC" />

 <uses-feature android:name="android.hardware.nfc" />

将执行Nfc读/写操作的Activity,在menifest.xml文件中的该活动中添加此intent过滤器:

<intent-filter>
        <action android:name="android.nfc.action.TAG_DISCOVERED" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

在您的活动onCreate()方法中,您必须初始化NFC适配器并定义Pending Intent:

 NfcAdapter mAdapter;
PendingIntent mPendingIntent;
mAdapter = NfcAdapter.getDefaultAdapter(this);   
if (mAdapter == null) {
    //nfc not support your device.
    return;
}
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
        getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

在onResume()中回调启用Foreground Dispatch以检测NFC意图。

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);

在onPause()回调中,您必须禁用forground dispatch:

    if (mAdapter != null) {
    mAdapter.disableForegroundDispatch(this);
}

在onNewIntent()回调方法中,您将获得新的Nfc Intent。获得The Intent后,您必须解析检测卡的意图:

 @Override
protected void onNewIntent(Intent intent){    
    getTagInfo(intent)
     }
private void getTagInfo(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
 //Start for Camera 
}