简体   繁体   English

当应用程序作为启动器启动时,Android NFC Intent无法正常工作

[英]Android NFC Intent not working when app is started as launcher

I'm developing an application which is allowed to be used as Launcher as well. 我正在开发一个允许用作Launcher的应用程序。 We use mainly Samsung tablet that have recently received an upgrade to Android 5.1.1 which seems to have changed how our App behaves when used as launcher. 我们主要使用最近收到Android 5.1.1升级的三星平板电脑,这似乎改变了我们的应用程序在用作启动器时的行为方式。

The problem is that Android seem to use the default com.android.nfc/.NfcRootActivity system default activity instead of our application. 问题是Android似乎使用默认的com.android.nfc/.NfcRootActivity系统默认活动而不是我们的应用程序。 It works fine when the application is started regular. 当应用程序定期启动时,它工作正常。 This was used to work before the Samsung deployed the update. 这在三星部署更新之前已经习惯了。

Here is log snipped . 这是日志剪切。

When started as launcher (NFC NOT WORKING) 当作为启动器启动时(NFC不工作)

 Line 474: E/NxpNfcJni( 1457): setReconnectState = 0x0
Line 476: D/NativeNfcTag( 1457): Starting background presence check
Line 478: D/NfcDispatcher( 1457): tryStartActivity. Send intent.
Line 480: D/PackageManager( 1014): Resolving for NFC Intent { flg=0x10008000 cmp=com.android.nfc/.NfcRootActivity (has extras) } flag 66688 user 0
Line 480: D/PackageManager( 1014): Resolving for NFC Intent { flg=0x10008000 cmp=com.android.nfc/.NfcRootActivity (has extras) } flag 66688 user 0
Line 480: D/PackageManager( 1014): Resolving for NFC Intent { flg=0x10008000 cmp=com.android.nfc/.NfcRootActivity (has extras) } flag 66688 user 0
Line 482: W/ResourcesManager( 1014): Asset path '/system/framework/com.broadcom.nfc.jar' does not exist or contains no resources.
Line 492: V/WindowManager( 1014): addAppToken: AppWindowToken{11f6a866 token=Token{513b8c1 ActivityRecord{951b5a8 u0 com.android.nfc/.NfcRootActivity t24}}} to stack=1 task=24 at 0
Line 492: V/WindowManager( 1014): addAppToken: AppWindowToken{11f6a866 token=Token{513b8c1 ActivityRecord{951b5a8 u0 com.android.nfc/.NfcRootActivity t24}}} to stack=1 task=24 at 0
Line 498: D/NfcPlugin( 1494): onPause Intent {  }
Line 502: D/NfcPlugin( 1494): stopNfc

And when started regular (WORKING) 当正常开始(工作)

 Line 261: E/NxpNfcJni( 1457): setReconnectState = 0x0
Line 263: D/PersonaManager( 1457): isNFCAllowed
Line 269: D/NativeNfcTag( 1457): Starting background presence check
Line 273: W/ActivityManager( 1014): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.nfc.action.TECH_DISCOVERED flg=0x24000000 cmp=com.bstmedia.xxx/yyy.KioskActivity (has extras) }
Line 277: D/NfcPlugin( 1494): onPause Intent {  }
Line 279: D/NfcPlugin( 1494): stopNfc

Here is what we have in the Manifest file. 这是我们在清单文件中的内容。

  <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|uiMode" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
        <intent-filter android:label="@string/launcher_name">
            <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" />
            <data android:mimeType="text/xxx" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>            
    </activity>

    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|uiMode" android:keepScreenOn="true" android:label="My App Name" android:launchMode="singleInstance" android:name="yyy.KioskActivity" android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </activity>

It seems that the foreground dispatch mode was not triggered when the device is restarted and the app opened as launcher. 当设备重新启动并且应用程序作为启动器打开时,似乎未触发前台调度模式。 We did not found a way to change this behavior. 我们没有找到改变这种行为的方法。

However a quick fix was to press the "recent app" button which than activated the forground dispatch mode for NFC once the app comes back to foreground. 然而,快速修复是按下“最近的应用程序”按钮,而不是在应用程序返回前台后激活NFC的forground dispatch模式。

We add the code from https://stackoverflow.com/a/32453115/2616377 to have this happen automatically on every reboot. 我们从https://stackoverflow.com/a/32453115/2616377添加代码,以便在每次重启时自动执行此操作。

I understand that this will not work with every device or android version. 我知道这不适用于每个设备或Android版本。 But we are happy to go this direction since the issue is specific to that particular Samsung update anyway. 但是我们很高兴能够朝着这个方向前进,因为问题特别针对特定的三星更新。

Are you using Cordova Kiosk Plugin? 你在使用Cordova Kiosk插件吗?

I had it done with BOOT_COMPLETE BroadcastReceiver: 我用BOOT_COMPLETE BroadcastReceiver完成了它:

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        final Context myContext = context;

        Timer timer = new Timer();
        timer.schedule(new TimerTask(){
            public void run() {
                KioskActivity.closeActivity(); //closeActivity() runs finish(); in KioskActivity
            }
        }, 10000);     

    }
}

and then in onPause in KioskActivity add: //KioskActivity.java 然后在KioskActivity中的onPause中添加://KioskActivity.java

ActivityManager activityManager = (ActivityManager) getApplicationContext()
                    .getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);

*Remember to register the BOOT_COMPLETED receiver in AndroidManifest.xml *请记住在AndroidManifest.xml中注册BOOT_COMPLETED接收器

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

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