Android的 - 听NFC适配器状态变化适配器、状态、Android、NFC

由网友(我是你高冷的爸爸)分享简介:我想建立它使用NFC的应用程序。我们的目标是要显示DialogFragment包含按钮链接去设置和手动更改它,当启用该功能,禁止DialogFragment。问题:如果用户启用/禁用使用NFC在下拉通知图标托盘,那么在onPause / onResume不会被调用的,完全忽略了条件。我相信有一个接收器,我可以注册代替...

我想建立它使用NFC的应用程序。我们的目标是要显示DialogFragment包含按钮链接去设置和手动更改它,当启用该功能,禁止DialogFragment。

问题:如果用户启用/禁用使用NFC在下拉通知图标托盘,那么在onPause / onResume不会被调用的,完全忽略了条件。 我相信有一个接收器,我可以注册代替,并实时作出适当的反应。任何想法,想法或引用将大大AP preciated!

下面code盘,如果状态启用/禁用。我也是在onResume事件应对它正确。

  NfcManager经理=(NfcManager)getSystemService(Context.NFC_SERVICE);
    NfcAdapter适配器= manager.getDefaultAdapter();

    如果(适配器= NULL和放大器;!&安培; adapter.isEnabled()){
        探测器=新NfcDetector(本);
        detector.setListener(本);
        onNfcFeatureFound();
    }
    其他 {
        onNfcFeatureNotFound();
    }
 

对于其他人看这个帖子,code以下将直接把用户进入设置,以启用/禁用NFC:

  startActivity(新意图(android.provider.Settings.ACTION_NFC_SETTINGS));
 

解决方案

想我应该张贴其他人寻找同样问题的答案,因为我没能找到一个很容易。

添加以下code到你的活动的onCreate()方法:

  IntentFilter的过滤器=新的IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
this.registerReceiver(mReceiver,过滤器);
 
使用舒适度

你的活动在

内部私有类中声明(或其他地方你想):

 私人最终的BroadcastReceiver mReceiver =新的BroadcastReceiver(){
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        最后弦乐行动= intent.getAction();

        如果(action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)){
            最终诠释状态= intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
                                                 NfcAdapter.STATE_OFF);
            开关(州){
            案例NfcAdapter.STATE_OFF:
                打破;
            案例NfcAdapter.STATE_TURNING_OFF:
                打破;
            案例NfcAdapter.STATE_ON:
                打破;
            案例NfcAdapter.STATE_TURNING_ON:
                打破;
            }
        }
    }
};

@覆盖
公共无效的onDestroy(){
    super.onDestroy();

    //删除广播监听器
    this.unregisterReceiver(mReceiver);
}

  //下列检查需要也可添加到所述onResume
@覆盖
保护无效onResume()
    super.onResume();
  //检查可用的NFC适配器
    PackageManager下午= getPackageManager();
    NfcManager经理=(NfcManager)getSystemService(Context.NFC_SERVICE);
    NfcAdapter适配器= manager.getDefaultAdapter();

    如果(适配器= NULL和放大器;!&安培; adapter.isEnabled()){
        createNfcDetector();
        onNfcFeatureFound();
    }
    其他 {
        onNfcFeatureNotFound();
    }
}
 

I am trying to build an application which uses NFC. The goal is to display a DialogFragment containing a button link to go the settings and change it manually and when the feature is enabled, disable the DialogFragment.

Problem: If the user enables/disables NFC using the icon in the pull down notifications tray , then the onPause/onResume doesn't get called and misses the condition entirely. I am sure there is a receiver that I can register to instead and respond appropriately in real time. Any ideas, thoughts or reference will be greatly appreciated!

The following code checks if the state is enabled/disabled. I am also responding to it appropriately in the onResume event.

    NfcManager manager = (NfcManager) getSystemService(Context.NFC_SERVICE);
    NfcAdapter adapter = manager.getDefaultAdapter();

    if(adapter != null && adapter.isEnabled()) {
        detector = new NfcDetector(this);
        detector.setListener(this);
        onNfcFeatureFound();
    }
    else {
        onNfcFeatureNotFound();
    }

For others looking at this post, the code below will take the user directly into settings to enable/disable NFC:

startActivity(new Intent(android.provider.Settings.ACTION_NFC_SETTINGS));

解决方案

Thought I should post the answer for other people looking for the same problem, since I wasn't able to find one easily.

Add the following code to your activities onCreate() method:

IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
this.registerReceiver(mReceiver, filter);

Inner private class declared within your activity (or anywhere else you like):

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
            final int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
                                                 NfcAdapter.STATE_OFF);
            switch (state) {
            case NfcAdapter.STATE_OFF:
                break;
            case NfcAdapter.STATE_TURNING_OFF:
                break;
            case NfcAdapter.STATE_ON:
                break;
            case NfcAdapter.STATE_TURNING_ON:
                break;
            }
        }
    }
};

@Override
public void onDestroy() {
    super.onDestroy();

    // Remove the broadcast listener
    this.unregisterReceiver(mReceiver);
}

  // The following check needs to also be added to the onResume
@Override
protected void onResume() 
    super.onResume();
  // Check for available NFC Adapter
    PackageManager pm = getPackageManager();
    NfcManager manager = (NfcManager) getSystemService(Context.NFC_SERVICE);
    NfcAdapter adapter = manager.getDefaultAdapter();

    if(adapter != null && adapter.isEnabled()) {
        createNfcDetector();
        onNfcFeatureFound();
    }
    else {
        onNfcFeatureNotFound();
    }
}

阅读全文

相关推荐

最新文章