【问题标题】:Can I read NFC tag in activity without restart it?我可以在不重新启动的情况下读取活动中的 NFC 标签吗?
【发布时间】:2015-01-21 09:20:31
【问题描述】:

我正在写一个相机应用程序,当手机读取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 标签时,它会调用 pause、new intent、resume。它关闭活动并重新开始,但这次它运行 onNewIntent 而不是 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 store找到了一个叫“NFC Camera”的App,它可以读取NFC tag而不需要重启activity,怎么做?

【问题讨论】:

  • 试试这个你可以再次进行相同的活动mNfcPendingIntent = PendingIntent.getActivity(CamTestActivits.this, 0, new Intent(CamTestActivity.this, CamTestActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  • 使用这个,它会调用:onPause > onCreate > onResume,所以还是会重启activity,而且这次没有调用onNewIntent,我无法通过tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);获取Tag对象

标签: android android-camera nfc


【解决方案1】:

在 Nfc 中,当新 Intent 被触发时,它将转到 onPause() 方法,然后转到 onNewIntent() 方法。当它转到 onNewIntent() 时,您将获得标签。当您获得标签时,您必须调用 new 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);
    }  
} 

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

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

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

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

<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() 回调中,您必须禁用前台调度:

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

在 onNewIntent() 回调方法中,您将获得新的 Nfc Intent。获取到 Intent 后,需要解析 Intent 来检测卡片:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 2011-11-11
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多