te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Android-Samsung-NFC tag read redirection to third party apps, how to handle only for my app - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Android-Samsung-NFC tag read redirection to third party apps, how to handle only for my app - Stack Overflow

programmeradmin4浏览0评论

I am using NFC tag read in my application, which is working 100% ok. On certain button click we read NFC tag and handle it accordingly

The problem is if user put NFC tag while anywhere in the application it auto triggers the third party application/default application which I don't want NFC hadnler should only open if intentionally by code not automatically

What I want is if user is in my application NFC read should only work on button click not anonymous, one button click all works fine but anonymous tag reading is creating problem

Platform: Android Device: Samsung Galaxy

Is there anything we can handle from code?

I am using NFC tag read in my application, which is working 100% ok. On certain button click we read NFC tag and handle it accordingly

The problem is if user put NFC tag while anywhere in the application it auto triggers the third party application/default application which I don't want NFC hadnler should only open if intentionally by code not automatically

What I want is if user is in my application NFC read should only work on button click not anonymous, one button click all works fine but anonymous tag reading is creating problem

Platform: Android Device: Samsung Galaxy

Is there anything we can handle from code?

Share Improve this question asked 2 days ago Tarun SeeraTarun Seera 4,4124 gold badges29 silver badges43 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Yes! You can control NFC behavior in your Android application to prevent it from being handled by third-party apps unless triggered explicitly within your app.

Solution: Disable Foreground Dispatch When Not Needed By default, NFC events are handled by the Android system and may trigger third-party apps. To prevent this, you should enable NFC foreground dispatch only when necessary (e.g., on button click) and disable it otherwise.

Steps to Achieve This: Enable Foreground Dispatch on Button Click Disable Foreground Dispatch When Not Needed Use a Foreground Activity to Prioritize Your App


  1. Setup Foreground Dispatch in Your Activity
@Override
protected void onResume() {
    super.onResume();
    enableForegroundDispatch();
}

@Override
protected void onPause() {
    super.onPause();
    disableForegroundDispatch();
}
  1. Enable Foreground Dispatch Only on Button Click
private void enableForegroundDispatch() {
    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
    if (adapter != null) {
        Intent intent = new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE);
        IntentFilter[] filters = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED)};
        String[][] techLists = new String[][]{};

        adapter.enableForegroundDispatch(this, pendingIntent, filters, techLists);
    }
}
  1. Disable Foreground Dispatch When Not Needed
private void disableForegroundDispatch() {
    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
    if (adapter != null) {
        adapter.disableForegroundDispatch(this);
    }
}
  1. Handle NFC Events Only When Needed
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (isNfcReadingEnabled) { // Only process NFC when enabled
        processNfcTag(intent);
    }
}
  1. Control NFC Read with a Button Click
private boolean isNfcReadingEnabled = false;

public void onNfcButtonClick(View view) {
    isNfcReadingEnabled = true;
    enableForegroundDispatch();
    Toast.makeText(this, "NFC Ready", Toast.LENGTH_SHORT).show();
}
  • Ensure your AndroidManifest.xml includes NFC permissions:
<uses-permission android:name="android.permission.NFC"/>
<uses-feature android:name="android.hardware.nfc" android:required="true"/>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论