试图忽略所有NFC意图与enableForegroundDispatch前景,而 [英] Trying to ignore all NFC intents while in foreground with enableForegroundDispatch

查看:3249
本文介绍了试图忽略所有NFC意图与enableForegroundDispatch前景,而的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的应用程序忽略NFC命令运行时 - 它通过与Android应用程序记录(AAR)的NFC标签启动,我不希望它能够通过推出时,其已经运行..我一直在努力,认真遵循别人的examples,但应用程序仍然能够(在前台)运行时,要由亚洲遥感协会发起。

的Manifest.xml:

 <应用>
    <活性GT;
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>
            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
        &所述;意图滤光器>
            <作用机器人:名字=android.nfc.action.NDEF_DISCOVERED/>
            <类机器人:名字=android.intent.category.DEFAULT/>
            <数据机器人:mime类型=应用/ com.MyApp.frontcam/>
        &所述; /意图滤光器>
    < /活性GT;
< /用途>

Activity.java:

 私人NfcAdapter mAdapter;
私人的PendingIntent mPendingIntent;
私人的IntentFilter [] mFilters;
私有String [] [] mTechLists;@覆盖
公共无效的onCreate(捆绑savedInstanceState){    mAdapter = NfcAdapter.getDefaultAdapter(本);
    mPendingIntent = PendingIntent.getActivity(此,0,新意图(本,的getClass())addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
    IntentFilter的NDEF =新的IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    尝试{
        ndef.addDataType(* / *);
    }
    赶上(MalformedMimeTypeException E){
        抛出新的RuntimeException(不及格,E);
    }
    mFilters =新的IntentFilter [] {NDEF,};
    mTechLists =新的String [] [] {
            新的String [] {NfcA.class.getName()},
            新的String [] {Ndef.class.getName()},
            新的String [] {NdefFormatable.class.getName()}
    };}@覆盖
保护无效onResume(){
    super.onResume();
    mAdapter.enableForegroundDispatch(这一点,mPendingIntent,NULL,NULL); //结束了设置mFilters和mTechLists为null
}


解决方案

当您使用 foregroundDispatch 当你扫描标签的新意图将给予您的活动。你应该覆盖 onNewIntent 方法。加入这样的:

MainActivity.java

 包com.example.nfctest;进口android.app.PendingIntent;
进口android.content.Intent;
进口android.nfc.NfcAdapter;
进口android.nfc.Tag;
进口android.os.Bundle;
进口android.support.v7.app.ActionBarActivity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.Toast;公共类MainActivity扩展ActionBarActivity {私人NfcAdapter mAdapter;
私人的PendingIntent mPendingIntent;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    mAdapter = NfcAdapter.getDefaultAdapter(本);
    mPendingIntent = PendingIntent.getActivity(此,0,新意图(本,的getClass())addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);    doSomethingWithIntent(this.getIntent());
}@覆盖
公共无效onResume(){
    super.onResume();
    //启用forground调度,以获得标签
    mAdapter.enableForegroundDispatch(这一点,mPendingIntent,NULL,NULL); //结束了设置mFilters和mTechLists为null
}@覆盖
公共无效的onPause(){
    super.onPause();
    //你需要在这里禁用forgroundDispatching
    mAdapter.disableForegroundDispatch(本);
}@覆盖
公共无效onNewIntent(意向数据){
    //抓住你的前景派遣推出的意图
    doSomethingWithIntent(数据);
}私人无效doSomethingWithIntent(意向数据)
{
    //从给定的意图获取标签
    标签标签= data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    如果(标记!= NULL)
    {
        //标签被发现
        Toast.makeText(这一点,享受您的标记。,3).show();
    }
    其他
    {
        //这是一个意图没有标签
        Toast.makeText(此,这是不带tag的意图。,3).show();
    }
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。操作栏会
    //自动处理上点击主页/向上按钮,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT ID = item.getItemId();
    如果(ID == R.id.action_settings){
        返回true;
    }
    返回super.onOptionsItemSelected(项目);
}
}

的Andr​​oidManifest.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.nfctest
    安卓版code =1
    机器人:=的versionName1.0>    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =21/>
 <使用许可权的android:NAME =android.permission.NFC/>
    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=。MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>< /清单>

您不需要定义 SINGLE_TOP 模式在您的清单中,因为你已经在这样做你的 mPendingIntent 。而我beleave,甚至当你离开这里的弗拉格来,它仍然会调用 onNewIntent 而不是的onCreate

I'm trying to get my app to ignore nfc commands while running - its launched by an NFC tag with an Android Application Record (AAR), and I dont want it to be able to be launched by that when its already running.. I've tried to carefully follow others examples, but the app is still able to be launched by AARs while running (in foreground).

Manifest.xml:

<application>
    <activity>
        <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"/>
            <data android:mimeType="application/com.MyApp.frontcam" />
        </intent-filter>
    </activity>
</application>

Activity.java:

private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;

@Override
public void onCreate(Bundle savedInstanceState) {

    mAdapter = NfcAdapter.getDefaultAdapter(this);
    mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("*/*");  
    }
    catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    mFilters = new IntentFilter[] {ndef, };
    mTechLists = new String[][] {
            new String[] { NfcA.class.getName() },
            new String[] { Ndef.class.getName() },
            new String[] { NdefFormatable.class.getName() }
    }; 

}

@Override 
protected void onResume() {
    super.onResume();
    mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); //ended up setting mFilters and mTechLists to null
}

解决方案

When you use foregroundDispatch a new intent will be given to your activity when you scan a tag. You should overwrite the onNewIntent method. Add something like this:

MainActivity.java

package com.example.nfctest;

import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAdapter = NfcAdapter.getDefaultAdapter(this);
    mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    doSomethingWithIntent(this.getIntent());        
}   

@Override
public void onResume() {
    super.onResume();
    //Enable forground dispatching to get the tags
    mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); //ended up setting mFilters and mTechLists to null
}   

@Override
public void onPause() {
    super.onPause();
    //You need to disable forgroundDispatching here
    mAdapter.disableForegroundDispatch(this);
}   

@Override
public void onNewIntent(Intent data) {
    //Catch the intent your foreground dispatch has launched
    doSomethingWithIntent(data);    
}

private void doSomethingWithIntent(Intent data) 
{ 
    //Get the tag from the given intent
    Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    if(tag != null)
    {
        //Tag is found
        Toast.makeText(this, "Enjoy your tag.", 3).show();      
    }
    else
    {
        //This was an intent without a tag
        Toast.makeText(this, "This was an intent without a tag.", 3).show();    
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nfctest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
 <uses-permission android:name="android.permission.NFC" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/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.intent.action.MAIN" />

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

</manifest>

You don't need to define the SINGLE_TOP mode in your manifest because you already did this in your mPendingIntent. And i beleave, even when you leave the flagg here to, it still will call the onNewIntent instead of the onCreate.

这篇关于试图忽略所有NFC意图与enableForegroundDispatch前景,而的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆