我正在写一个相机应用程序,当手机读取到 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。它关闭 Activity 并重新开始,但这次它运行 onNewIntent 而不是新的 onCreate
我试了很多flag,但是没有人能把activity保持在前台
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”,它可以读取 NFC 标签而无需重新启动 Activity ,该怎么做?
最佳答案
在 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);
在那个 Activity 中,这里的数据会来:
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 过滤器:
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
在您的 Activity 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() 回调中启用前台调度以检测 NFC Intent 。
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
}
关于android - 我可以在不重启的情况下读取 Activity 中的 NFC 标签吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28063480/