当应用程序打开时,Android NFC始终会调用onCreate

时间:2015-08-27 13:15:33

标签: android android-intent nfc oncreate activity-lifecycle

我制作了检测nfc标签的应用。当我的应用关闭时,所有工作正常,每次我通过触摸nfc标记到我的手机来启动我的应用。

08-27 16:02:12.121  16692-16692/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onCreate
08-27 16:02:12.121  16692-16692/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onResume
08-27 16:02:14.556  16692-16692/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onPause
08-27 16:02:14.571  16692-16692/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onNewIntent
08-27 16:02:14.576  16692-16692/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onResume
08-27 16:02:20.216  16692-16692/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onPause
08-27 16:02:20.221  16692-16692/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onNewIntent
08-27 16:02:20.226  16692-16692/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onResume

但是当我的应用程序打开时(通过触摸菜单中的应用程序图标),当我触摸nfc标记到我的手机onCreate时,每次触摸nfc标记到手机时都会调用此方法。这是我的日志

08-27 16:10:41.316  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onCreate
08-27 16:10:41.316  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onResume
08-27 16:10:45.581  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onPause
08-27 16:10:45.631  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onCreate
08-27 16:10:45.631  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onResume
08-27 16:10:47.881  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onPause
08-27 16:10:47.906  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onNewIntent
08-27 16:10:47.906  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onResume
08-27 16:10:50.076  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onPause
08-27 16:10:50.121  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onCreate
08-27 16:10:50.121  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onResume
08-27 16:10:57.216  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onPause
08-27 16:10:57.236  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onNewIntent
08-27 16:10:57.236  24218-24218/v2.nfc.android.com.nfcv2 D/MainActivity﹕ onResume

我不知道为什么会这样。

这是我的AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是我的完整活动代码

public class MainActivity extends Activity {

    private NfcAdapter adapter;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = NfcAdapter.getDefaultAdapter(this);

        TextView t = (TextView) findViewById(R.id.text);
        t.setText(Math.random()+"");

        Log.d(TAG, "onCreate");

    }

    @Override
    protected void onResume() {
        super.onResume();

        if((NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction()))){
            enableForegroundDispatch();
        }

        Log.d(TAG, "onResume");
    }

    private void enableForegroundDispatch() {
        Intent intent = new Intent(getApplicationContext(), MainActivity.class).addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

        IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

        try {
            intentFilter.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            e.printStackTrace();
        }

        if (adapter != null){
            adapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{intentFilter}, null);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        adapter.disableForegroundDispatch(this);

        Log.d(TAG, "onPause");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        if (isNfcIntent(intent)) {
            String action = intent.getAction();
            Toast.makeText(MainActivity.this, action, Toast.LENGTH_SHORT).show();
        }

        Log.d(TAG, "onNewIntent");
    }

    private boolean isNfcIntent(Intent intent){
        return intent.hasExtra(NfcAdapter.EXTRA_TAG);
    }
}

0 个答案:

没有答案