开发者

How to force one activity with same intent filters in Android?

i am making a Android App with discovery nfc. When i discovery a NF开发者_开发技巧C tag, i have to select the application (Default android app and my app).

What i can do in my app for Android system dont ask what app i what open? My idea is launch my app for default.

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


It seems on Android an application itself cannot set itself as the default handler, and instead rely on the user to chose the action, if there is more than one choice and no default specified.

That application selection box should have a check box that says something along the lines of "Use this application by default". It is up to the user to check that box if they want to use your application every time.


Use the "data" element in the intent filter to define a custom MIME type.

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <data android:mimeType="application/com.example.package" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

You can add this intent filter to any activity you want to launch. You can even add this as an additional filter to the launcher activity along with the launch filter.

If you do so, you can use the following code to determine if your activity was launched by NFC or by launch icon:

Intent intent = getIntent();
if (intent.getType() != null && intent.getType().equals("application/com.example.package")) {
// app is launched from NFC
} else {
// app is launched from launcher icon
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜