android - TabActivity 上的 NFC Intent 启动新 Activity 而不是刷新现有 Activity

标签 android refresh nfc tabactivity android-pendingintent

my old question之后考虑到您给我的建议,我现在再次尝试...

我有一个 Android 测试应用程序,我想在其中使用 NFC 读取某些内容(例如 RFID 芯片)后立即将文本更改为 TextView。

问题是我的 TextView Activity 位于 TabHost 的 Tab 中。当 NFC 正在读取某些内容时,Activity 在前台启动并且 TextView 没有更改。 我想要的是只有 TextView 发生变化,其他一切都保持原样....

这是我的代码:

我的 TabActivity:

public class NfcTabsActivity extends TabActivity {

    private NfcAdapter nfc;
    private PendingIntent nfcintent;
    private String[][] nfctechfilter = new String[][] { new String[] { NfcA.class.getName() } };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TabHost tabHost = getTabHost();

        Intent intent = new Intent().setClass(this, NfcTest.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        TabSpec spec = tabHost.newTabSpec("nfctab").setIndicator("NFC").setContent(intent);
        tabHost.addTab(spec);

        nfc = NfcAdapter.getDefaultAdapter(this);

            // PendingIntent using the NfcTest-Activity to receive the Intent. (Am I doing this correctly??)
        nfcintent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
    }

    // Start looking for NFC when activity is started/resumed.
    @Override
    protected void onResume() {
        super.onResume();
        nfc.enableForegroundDispatch(this, nfcintent, null, nfctechfilter);
    }

    // Disable NFC when leaving activity
    @Override
    protected void onPause() {
        super.onPause();
        nfc.disableForegroundDispatch(this);
    }
}

这里是 我的 NfcTest Activity ,它在使用 NFC 时应该接收 intent:

public class NfcTest extends Activity {

    private TextView status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.nfctest);

        status = (TextView) findViewById(R.id.textView2);

    }

    @Override
    public void onNewIntent(Intent intent) {

        status.setText("RFID detected...");

    }
}

感谢 NFC 专家 建议将 PendingIntent 放入 TabActivity 中! 不幸的是 - 正如我在另一个线程中所说 - 这对我来说都不起作用...... :( 也许我在我的代码中做错了什么?

这是我的 AndroidManifest.xml 中的 Activity 定义:

        [...]
        <activity android:name=".NfcTest" android:clearTaskOnLaunch="true" android:alwaysRetainTaskState="true" android:finishOnTaskLaunch="true"></activity>
        [...]

谁能帮我解决这个问题? 也许你是 NFC 专家?也许我只是理解你的想法有问题或者在我的代码中混淆了一些东西......? :/

最佳答案

试试这个

        Intent intent = new Intent().setClass(this, NfcTest.class);

    TabSpec spec = tabHost.newTabSpec("nfctab").setIndicator("NFC").setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    tabHost.addTab(spec);

关于android - TabActivity 上的 NFC Intent 启动新 Activity 而不是刷新现有 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12378857/

相关文章:

android - 通知取消问题

禁用启动器 Activity 后,android 重新安装失败

Django:登录用户并在同一页面上刷新而不定义模板?

android - 布局 setMargins 在横向模式下不起作用

android - 确定移动设备的最大浏览器高度(本地址栏未显示时)

java - 在其他 JPanel 内重新绘制 JPanel

javascript - 使用chart.js刷新图表的图像

android - 不可见的 Ndef 消息

java - TextView 类型未定义方法 getBytes()

android - 是否可以通过 NFC 一键连接两个 Android 设备交换数据?