Android 当应用作为启动器启动时 NFC Intent 不工作
Android NFC Intent not working when app is started as launcher
我正在开发一个也可以用作启动器的应用程序。我们主要使用最近升级到 Android 5.1.1 的三星平板电脑,这似乎改变了我们的应用程序用作启动器时的行为方式。
问题是 Android 似乎使用默认 com.android.nfc/.NfcRootActivity
系统默认 activity 而不是我们的应用程序。当应用程序正常启动时,它工作正常。在三星部署更新之前,这曾用于工作。
这是截取的日志。
作为启动器启动时(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
当开始正常时(工作)
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
这是清单文件中的内容。
<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>
当设备重新启动并且应用程序作为启动器打开时,似乎没有触发前台调度模式。我们没有找到改变这种行为的方法。
然而,一个快速的解决方法是按下 "recent app" 按钮,该按钮会在应用程序返回前台后激活 NFC 的前台调度模式。
我们添加了 中的代码,以便在每次重新启动时自动执行此操作。
我知道这不适用于所有设备或 android 版本。但我们很高兴朝这个方向前进,因为这个问题无论如何都是特定于三星更新的。
您在使用 Cordova Kiosk 插件吗?
我用 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);
}
}
然后在 KioskActivity 的 onPause 添加:
//KioskActivity.java
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);
*记得在AndroidManifest.xml
中注册BOOT_COMPLETED接收器
我正在开发一个也可以用作启动器的应用程序。我们主要使用最近升级到 Android 5.1.1 的三星平板电脑,这似乎改变了我们的应用程序用作启动器时的行为方式。
问题是 Android 似乎使用默认 com.android.nfc/.NfcRootActivity
系统默认 activity 而不是我们的应用程序。当应用程序正常启动时,它工作正常。在三星部署更新之前,这曾用于工作。
这是截取的日志。
作为启动器启动时(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
当开始正常时(工作)
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
这是清单文件中的内容。
<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>
当设备重新启动并且应用程序作为启动器打开时,似乎没有触发前台调度模式。我们没有找到改变这种行为的方法。
然而,一个快速的解决方法是按下 "recent app" 按钮,该按钮会在应用程序返回前台后激活 NFC 的前台调度模式。
我们添加了 中的代码,以便在每次重新启动时自动执行此操作。
我知道这不适用于所有设备或 android 版本。但我们很高兴朝这个方向前进,因为这个问题无论如何都是特定于三星更新的。
您在使用 Cordova Kiosk 插件吗?
我用 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);
}
}
然后在 KioskActivity 的 onPause 添加: //KioskActivity.java
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);
*记得在AndroidManifest.xml
中注册BOOT_COMPLETED接收器