简体   繁体   English

意图过滤器不适用于Android Beam NFC

[英]Intent Filter not working for Android Beam NFC

I am sure this is simple but I cannot figure it out. 我确信这很简单,但我无法弄清楚。 All I am trying to do is send a message via NFC (android Beam) and open my App on the receiver device. 我要做的就是通过NFC(android Beam)发送一条消息,然后在接收器设备上打开我的App。 I did test my code on a new Project and it worked perfectly but if I try it on my real Project it just start "New Tag collected" and shows application/eu.freemoser.mydebts2go (see my screenshot ). 我确实在一个新项目上测试了我的代码,但它确实运行良好,但是如果在我的真实项目上尝试它,则只需启动“收集了新标签”,并显示application / eu.freemoser.mydebts2go(请参阅我的屏幕截图 )。 I don't know what's wrong maybe something with the manifest? 我不知道这是怎么回事清单可能有问题吗? The Google results doesn't match with my problem (or I am just to stupid) but I believe I found some related answer unfortunately I still was not able to solve my problem Google的结果与我的问题不符(或者我只是愚蠢),但我相信我发现了一些相关的答案,很遗憾,我仍然无法解决我的问题

My Manifest: 我的清单:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="eu.freemoser.myDebts2go"
        android:versionCode="16"

        android:versionName="1.1.0">

        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.NFC" />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
        <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
        <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

        <uses-feature android:name="android.hardware.nfc" android:required="false" />
        <uses-feature android:name="android.hardware.camera" />



        <uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="19" />
        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true" />

        <application
            android:name="eu.freemoser.myDebts2go.MyApplication"
            android:allowBackup="true"
            android:hardwareAccelerated="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/Theme.Mydebts2go">
            >


            <service android:name="eu.freemoser.myDebts2goService.NotificationService"></service>
            <service android:name="eu.freemoser.myDebts2goService.SynchronizationService"></service>
            <service android:name="eu.freemoser.myDebts2goService.SynchronizService"></service>
            <service android:name="eu.freemoser.myDebts2goService.AwesomeSynchronizService"></service>


            <activity
                android:name="eu.freemoser.myDebts2go.MainActivity"
                android:label="@string/app_name"
                android:screenOrientation="portrait">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>


            <activity
           android:name="eu.freemoser.myDebts2GoActivities.AwesomeDetailActivity"
                android:screenOrientation="portrait" android:theme="@style/Theme.MyDebts2GO.Detail">
            </activity>

            <activity android:name=".AndroidBeamActivity" android:screenOrientation="portrait"
                android:theme="@android:style/Theme.DeviceDefault.Wallpaper.NoTitleBar">
                <intent-filter>
                    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <data android:mimeType="application/eu.freemoser.myDebts2go" />
                </intent-filter>
            </activity>

            <activity android:name="eu.freemoser.myDebts2GoActivities.DriveRestoreActivity"></activity>
            <activity android:name="eu.freemoser.myDebts2GoActivities.SynchronizActivity"></activity>

            <activity android:name="eu.freemoser.myDebts2GoActivities.SettingActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MANAGE_NETWORK_USAGE" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>

            <activity android:name="eu.freemoser.myDebts2GoActivities.DatePickerActivity"></activity>
            <activity android:name="eu.freemoser.myDebts2GoActivities.LocationPickerActivity"></activity>

            <activity android:name="eu.freemoser.myDebts2GoActivities.SearchActivity">
                <intent-filter>
                    <action android:name="android.intent.action.SEARCH" />
                </intent-filter>
            </activity>

            <activity
                android:name="eu.freemoser.myDebts2GoActivities.DriveAuthorzingActivity"
                android:theme="@android:style/Theme.Translucent.NoTitleBar">
            </activity>

            <activity
                android:name="eu.freemoser.myDebts2GoActivities.FastModusShortcutActivity"
                android:exported="true"
                android:theme="@android:style/Theme.Holo.Light.NoActionBar">
            </activity>


            <!-- android:value="API_KEY" /> DEBUG-->

            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="API_KEY" />
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="VERSION" />


        </application>


    </manifest>

The Fragment (sender) 片段(发送者)

public class AwesomeDetailFragment extends Fragment implements ObservableScrollView.Callbacks, NfcAdapter.CreateNdefMessageCallback, NfcAdapter.OnNdefPushCompleteCallback {
    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       ...
        setUpAndroidBeam();
        return mRootView;
    }

    private void setUpAndroidBeam() {
        PackageManager pm = getActivity().getPackageManager();
        // Check whether NFC is available on device
        if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
            // NFC is not available on the device.

        }
        // Check whether device is running Android 4.1 or higher
        else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            // Android Beam feature is not supported.

        } else {
            myNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
            manageNfc();

        }
    }

    private void manageNfc() {
        if (myNfcAdapter != null) {
            myNfcAdapter.setNdefPushMessageCallback(this, getActivity());
            myNfcAdapter.setOnNdefPushCompleteCallback(this, getActivity());
        }
    }

    ...

    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        Time time = new Time();
        time.setToNow();
        String text = myBetrag + "//" + myTitle + "//" + myContactName + "//" + myStatus + "//" + myDebtDate + "//" + myCreateDate + "//" + myRemamberDate + "//" + myNote;
        NdefMessage msg = new NdefMessage(
                new NdefRecord[]{createMimeRecord(
                        "application/eu.freemoser.myDebts2go", text.getBytes())});
        return msg;
    }

    public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
        byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
        NdefRecord mimeRecord = new NdefRecord(
                NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
        return mimeRecord;
    }

    @Override
    public void onNdefPushComplete(NfcEvent event) {

    }
}

The Activity (receiver) 活动(接收方)

public class Beam extends Activity {
    private Long userID = null;
    private Long adressID = null;

    private DBAdapter myDb;
    //NFC
    private String myBetrag;
    private String myTitle;
    private String myContactName;
    private String myDebtDate;
    private String myCreateDate;
    private String myStatus;
    private String myRemamberDate;
    private String myNote;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
    }

    void processIntent(Intent intent) {
        myDb = new DBAdapter(this);
        myDb.open();
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
        // only one message sent during the beam
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        // record 0 contains the MIME type, record 1 is the AAR, if present
        String temp = new String(msg.getRecords()[0].getPayload());
        String[] arrrayTemp = temp.split("//");
        try {
            myBetrag = arrrayTemp[0];
            myTitle = arrrayTemp[1];
            myContactName = arrrayTemp[2];
            myStatus = arrrayTemp[3];
            myDebtDate = arrrayTemp[4];
            myCreateDate = arrrayTemp[5];
            // can be "NOT"
            myRemamberDate = arrrayTemp[6];
            myNote = arrrayTemp[7];
            // checkValues
            channgeStatus();
            checkIfNot();
            checkUser();
            //write to database
            write();

        } catch (Exception ex) {
            this.finish();
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myDb.close();
    }

    private void checkUser() {
        //do some stuff
       ...
    }

    private void write() {

    //do some stuff
       ...

    }

    private void checkIfNot() {
    //do some stuff
       ...
    }

    private void channgeStatus() {
    //do some stuff
       ...
    }

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }

}

Android's MIME type matching for intent filters is case-sensitive (eventhough the MIME types themselves are not). Android的Intent过滤器的MIME类型匹配区分大小写(尽管MIME类型本身不区分大小写)。 Therefore, with Android (and also pretty much everywhere you use them) you should stick to the convention to use MIME types with lowercase letters only. 因此,对于Android(以及几乎所有使用它们的地方),您都应该遵守约定,仅将MIME类型与小写字母一起使用。

Specifically with MIME type records received over NFC, Android will automatically convert them to all-lowercase letters to overcome the problem of case-sensitivity in intent filters. 特别是对于通过NFC接收到的MIME类型记录,Android会自动将它们转换为全小写字母,以克服意图过滤器中区分大小写的问题。 So in your example, changing the type name in the intent filter to "application/eu.freemoser.mydebts2go" should work: 因此,在您的示例中,将意图过滤器中的类型名称更改为"application/eu.freemoser.mydebts2go"应该起作用:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="application/eu.freemoser.mydebts2go"/>
</intent-filter>

In addition, you should also make sure you send the MIME type in lowercase letters only: 此外,还应确保仅以小写字母发送MIME类型:

NdefMessage msg = new NdefMessage(
            new NdefRecord[]{createMimeRecord(
                    "application/eu.freemoser.mydebts2go", text.getBytes())});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM