在键盘服务中我无法启动主要活动:
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");
}
结果,弹出错误
logcat的:
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)
答案 0 :(得分:1)
在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");
}
答案 1 :(得分: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 = new Intent("android.intent.action.MAINACTIVITY");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent);
试试这个..