嘿,我正在努力檢測我的應用程式中的自定義鍵盤。我成功地做到了這一點,并通過使用代碼檢測用戶是自定義鍵盤
fun isUsingCustomKeyboard(context: Context): Boolean {
val imm: InputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val inputMethodProperties: List<InputMethodInfo> = imm.enabledInputMethodList
for (i in 0..inputMethodProperties.size) { // loop to every input method
val inputMethodInfo = inputMethodProperties[i]
val inputMethodId = Settings.Secure.getString(
context.contentResolver,
Settings.Secure.DEFAULT_INPUT_METHOD
)
if (inputMethodInfo.id.equals(inputMethodId)) {
if (inputMethodInfo.serviceInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM == 0) {
return true
}
break
}
}
return false
}
但是我收到了這個錯誤,有人能告訴我上面的代碼出了什么問題嗎
com.example.android.KeyboardHelper.isUsingCustomKeyboard (KeyboardHelper.kt:15)
com.example.android.trackKeyboardType (SignInActivity.kt:75)
com.example.android.onCreate (SignInActivity.kt:71)
android.app.Activity.performCreate (Activity.java:8207)
android.app.Activity.performCreate (Activity.java:8191)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1309)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3808)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:4011)
android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:85)
android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2325)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:246)
android.app.ActivityThread.main (ActivityThread.java:8633)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
uj5u.com熱心網友回復:
for (i in 0..inputMethodProperties.size) {size 給出串列中的物件數,但索引從 0 開始,因此當您嘗試讀取最后一個超出范圍時。size -1 是最后一個索引。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/403186.html
標籤:
