遇到的问题:framework中定制四色键,但按键时无反应,报Calling startActivity() from outside of an Activity context requires

查看log:

 01-16 14:51:14.595 W/System.err( 3938): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
01-16 14:51:14.595 W/System.err( 3938): at android.app.ContextImpl.startActivity(ContextImpl.java:1124)
01-16 14:51:14.595 W/System.err( 3938): at android.app.ContextImpl.startActivity(ContextImpl.java:1111)
01-16 14:51:14.595 W/System.err( 3938): at com.android.internal.policy.impl.JumpProcessor.jumpApp(JumpProcessor.java:36)
01-16 14:51:14.595 W/System.err( 3938): at com.android.internal.policy.impl.JumpProcessor.startIPTVEPG(JumpProcessor.java:20)
01-16 14:51:14.595 W/System.err( 3938): at com.android.internal.policy.impl.JumpProcessor.clickLive(JumpProcessor.java:108)
01-16 14:51:14.595 W/System.err( 3938): at com.android.internal.policy.impl.PhoneWindowManager.interceptKeyBeforeDispatching(PhoneWindowManager.java:2775)
01-16 14:51:14.595 W/System.err( 3938): at com.android.server.wm.InputMonitor.interceptKeyBeforeDispatching(InputMonitor.java:374)
01-16 14:51:14.595 W/System.err( 3938): at com.android.server.input.InputManagerService.interceptKeyBeforeDispatching(InputManagerService.java:1388)
01-16 14:51:14.595 W/System.err( 3938): at dalvik.system.NativeStart.run(Native Method)
01-16 14:51:14.595 D/RemoteIME( 5990): keycode: 183, realAction: false


查看代码

try {
            Log.d(TAG,"packagename=" + packageName + " ,key=" + key + " ,value=" + value);
            PackageManager packageManager = context.getPackageManager();
            Intent intent = packageManager.getLaunchIntentForPackage(packageName);
            if (intent != null) {
               // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
                    if (packageName.equals("")) {
                        value = value + "desktop=zy";
                        Log.d(TAG,"value=" + value);
                    }
                    intent.putExtra(key, value);
                }
                context.startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

报错提示:

Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag.

错误分析:

Context中有一个startActivity方法,Activity继承自Context,重载了startActivity方法。如果使用Activity的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的話,就需要开启一个新的的task,遇到这个异常,是因为使用了Context的startActivity方法。解决办法是,加一个flag。
代码:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
这样就可以在新的task里面启动这个Activity了。

 // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);


你可能感兴趣的:(Android开发,framework,Calling,startActivity,from,o)