Android NFC意图用于通知而不是活动

时间:2014-09-18 21:39:04

标签: android android-intent android-activity notifications nfc

有没有办法让nfc标签显示通知而不是打开实际活动?

我需要将我的应用放在nfc标签上时显示,而是显示通知。我用google搜索了一下,但找不到任何东西,因此代码不多。

我目前已将我的应用设置为在发现Ndef标记时打开主要活动。

前台调度程序已启用且有效,我只需要知道一种显示通知的方法,而不是实际的应用活动。

MainActivity onCreate:

IntentFilter ndef = new IntentFilter( NfcAdapter.ACTION_NDEF_DISCOVERED );
try {
    ndef.addDataType( "*/*" );
}
catch (MalformedMimeTypeException e) {
    throw new RuntimeException( "fail", e );
}

nfcFilters = new IntentFilter[] { ndef, };
nfcTechList = new String[][] {
        new String[] { NfcA.class.getName() },
        new String[] { NfcB.class.getName() },
        new String[] { NfcV.class.getName() },
        new String[] { NfcF.class.getName() },
        new String[] { Ndef.class.getName() },
        new String[] { NdefFormatable.class.getName() },
        new String[] { MifareClassic.class.getName() },
        new String[] { MifareUltralight.class.getName() },
        new String[] { IsoDep.class.getName() },
        new String[] { NfcBarcode.class.getName() },
    };

清单:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:noHistory="true"
    android:windowSoftInputMode="adjustResize|stateHidden"
    android:screenOrientation="portrait" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

</activity>

一款名为Trigger的应用程序可以满足我的需求。当您设置任务并关闭应用程序,然后将应用程序放在nfc标记上时,应用程序不会弹出并显示通知。

知道我是否可以将它们分开以便在应用关闭时显示通知也是有用的,但如果它正在运行它应该在

中执行某些操作

编辑:

好吧,我找到了办法做到这一切。我这样做的方法是让另一个活动听取Ndef的发现。现在,为了使应用程序打开新的Activity,我不得不添加一个带有mime类型的app应用程序包的自定义mime类型(TNF_MIME_MEDIA)的记录。我还需要一份应用记录作为NFC标签中的最后一条记录。

现在应用程序将启动我创建的新活动。 在这个Activity的onCreate方法中,我只是创建了通知,然后运行了finish()。

之前我没有这样做,因为我认为这样做会显示活动,显示通知,然后立即关闭它(关闭之前打开的所有应用)。 但事实并非如此,所以效果很好。

有人可以告诉我这是解决问题的好方法还是坏方法。

0 个答案:

没有答案