0

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

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)
4

2 回答 2

1

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

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");
} 
于 2012-12-03T07:04:20.913 回答
1

改变Manifest.xml

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

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

作为

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

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

并从您的 Intent 中调用,...

        Intent intent = new Intent("android.intent.action.MAINACTIVITY");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        startActivity(intent);

试试这个..

于 2012-12-03T07:07:17.927 回答