活动不是从NFC_TECH_DISCOVERED-intent过滤器开始的

时间:2016-05-17 15:39:31

标签: android android-intent android-activity nfc

我正在尝试通过NFC读取一些信息,并创建了这样做的intent-filter,以及包含所需技术的xml。意图过滤器应该启动我的ReadActivity,但当我将卡放在手机附近时它不会这样做。 NFC被激活,所以这应该不是问题。我真的没有看到我的代码有问题,所以如果有人可以看看它并且可能给我一个正确方向的暗示会很棒。这是代码:

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.johan.nfcreaderforunicard">
    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true"/>
    <uses-sdk android:minSdkVersion="10"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/apple"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".ReadActivity">
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/tech"/>
        </activity>
</manifest>

ReadActivity:

import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;
import android.os.Bundle;

import java.io.IOException;

public class ReadActivity extends AppCompatActivity {

    private final byte[] selectAid = {(byte)90, (byte)95, (byte)-124, (byte)21}; 
    private final byte[] creditPayload = {(byte)108, (byte)1};                        

    private byte[] resultOk;
    private byte[] creditBytes;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.read_view);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        if(NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())){
            IsoDep isodep = IsoDep.get((Tag)getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG));
            if(isodep != null){
                try{
                    isodep.connect();
                    resultOk = isodep.transceive(selectAid);
                    if(resultOk[0] == 0){
                        creditBytes = isodep.transceive(creditPayload);
                    }
                }catch (IOException e){
                }
            }
        }
        float credit = (float)formatCredit(creditBytes);
        TextView label = (TextView)findViewById(R.id.read_text);
        label.setText("Dein Guthaben: "+String.valueOf(credit)+"€");
    }

    private double formatCredit(byte[] array) {
        double credit = (double)(((0xff & array[4]) << 24) + ((0xff & array[3]) << 16) + ((0xff & array[2]) << 8) + (0xff & array[1])) / 1000D;
        return credit;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem){
        switch(menuItem.getItemId()){
            case(R.id.action_settings):
                Intent startSettings = new Intent(ReadActivity.this, SettingsActivity.class);
                startActivity(startSettings);
                return true;
            case(R.id.about):
                Intent startAbout = new Intent(ReadActivity.this, AboutActivity.class);
                startActivity(startAbout);
                return true;
            default:
                return super.onOptionsItemSelected(menuItem);
        }
    }

    @Override
    public void onBackPressed(){
        Intent backToMain = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(backToMain);
        finish();
    }
}

MainActivity:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem){
        switch(menuItem.getItemId()){
            case(R.id.action_settings):
                Intent startSettings = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(startSettings);
                return true;
            case(R.id.about):
                Intent startAbout = new Intent(MainActivity.this, AboutActivity.class);
                startActivity(startAbout);
                return true;
            default:
                return super.onOptionsItemSelected(menuItem);
        }
    }

    @Override
    public void onBackPressed(){
        finish();
    }
}

AboutActivity和SettingsActivity还没有任何内容,所以我没有在这篇文章中包含它们。我真的不知道问题出在哪里。

1 个答案:

答案 0 :(得分:0)

重新启动手机和NFC设置解决了这个问题。