简体   繁体   English

Android Intent重新开启活动

[英]Android Intent reopening activity

I have an issue that I have not found a solution to on this site, but if this is a duplicate question, I apologize. 我有一个问题,我在该站点上找不到解决方案,但是如果这是一个重复的问题,我深表歉意。

I am developing an application that serves as a terminal for registering when employees start/finish work, among numerous other things. 我正在开发一个应用程序,该应用程序可作为员工开始/完成工作时进行注册的终端。 The way it works is that with NFC switched-on, they scan their NFC cards and my app reads them and ultimately sends the appropriate information to the server. 它的工作方式是:打开NFC时,他们会扫描NFC卡,然后我的应用程序会读取它们,并最终将适当的信息发送到服务器。

However, if the app is already open (it's supposed to be open all the time, so this is an issue) and an NFC card is scanned, it reopens the app. 但是,如果该应用程序已经打开(应该一直处于打开状态,所以这是一个问题),并且扫描了NFC卡,它将重新打开该应用程序。 Of course, this is done because I have set it that way in the manifest. 当然,这样做是因为我在清单中设置了这种方式。 But I can not find a way to have my app recieve the NFC scan intent if I do not add all of these lines in the manifest: 但是,如果我未在清单中添加所有这些行,则无法找到让我的应用程序接收NFC扫描意图的方法:

<intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED" />

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

I have tried just writing without the but in that case it does not read the card, but instead the program chooser comes up on the phone, or if the phone does not have an appropriate app it simply says "NFC read error" . 我试过不带,但是在这种情况下它不会读取卡,而是在电话上显示程序选择器,或者,如果电话没有合适的应用程序,它只会说"NFC read error"

Does anyone have a solution for this? 有人对此有解决方案吗? This is the last step in my project, and I have had a lot of trouble with it, and would appreciate any help. 这是我项目的最后一步,我遇到了很多麻烦,希望对您有所帮助。 It's probably something simple that I'm just not seeing, but I'd appreciate it either way. 我可能没有看到过这很简单的东西,但是无论哪种方式我都会很感激。

Android activities have different launch modes. Android活动具有不同的启动模式。 If you set single instance it will use already opened activity and doesn't create a new activity. 如果设置单个实例,它将使用已经打开的活动,并且不会创建新的活动。 You can read the new intent in override method onNewIntent() 您可以在重写方法onNewIntent()读取新的意图

protected void onNewIntent(Intent intent) {
  super.onNewIntent(intent);
  // read intent values here
}

For various activity elements 对于各种活动元素

You can use broadcastReceiver, - first initiate the receiver to your activity 您可以使用broadcastReceiver,-首先启动您的活动的接收器

IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("whateveryouwant");

    notificationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
// here you can read the intent and customize the action;
            int usage = intent.getIntExtra("usage",1000); 


            }


        }
    };
  • second register the broadcast 第二次注册广播

    registerReceiver(notificationBroadcastReceiver,intentFilter); registerReceiver(notificationBroadcastReceiver,intentFilter);

  • At end unregister to the broadcast in the onDestroy method 最后,使用onDestroy方法取消注册到广播

    @Override protected void onDestroy() { @Override受保护的void onDestroy(){

     if(notificationBroadcastReceiver != null){ unregisterReceiver(notificationBroadcastReceiver); notificationBroadcastReceiver = null; } super.onDestroy(); 

    } }

after doing that instead of intenting activity you can sendBroadcast() 完成此操作后,您可以发送sendBroadcast()而不是进行活动

a little guide: https://developer.android.com/reference/android/content/BroadcastReceiver.html 一点指南: https : //developer.android.com/reference/android/content/BroadcastReceiver.html

hope it will be helpfull 希望对您有所帮助

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

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