gpt4 book ai didi

java - 无法从服务调用主 Activity

转载 作者:行者123 更新时间:2023-12-02 07:25:04 27 4
gpt4 key购买 nike

在键盘服务中我无法启动主要 Activity :

public void onKey(int primaryCode, int[] keyCodes) 
Log.d(TAG, primaryCode + " PrimaryCode to send");
if (primaryCode == 6) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Log.d(TAG, primaryCode + " Greating is command");
}

结果弹出错误

日志猫:

12-03 10:46:07.613: E/InputEventReceiver(2776): Exception dispatching input event.
12-03 10:46:07.613: E/MessageQueue-JNI(2776): Exception in MessageQueue callback: handleReceiveCallback
12-03 10:46:07.613: E/MessageQueue-JNI(2776): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
12-03 10:46:07.613: E/MessageQueue-JNI(2776): at android.app.ContextImpl.startActivity(ContextImpl.java:944)
12-03 10:46:07.613: E/MessageQueue-JNI(2776): at android.app.ContextImpl.startActivity(ContextImpl.java:931)
12-03 10:46:07.613: E/MessageQueue-JNI(2776): at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
12-03 10:46:07.613: E/MessageQueue-JNI(2776): at sample.stand.Keyb.onKey(Keyb.java:162)

最佳答案

在 logCat 中,他建议使用 FLAG_ACTIVITY_NEW_TASK 标志,因此请尝试以下操作:

if (primaryCode == 6) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Log.d(TAG, primaryCode + " Greating is command");
}

关于java - 无法从服务调用主 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13678557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com