Android-- 解决移动魔百盒CM201-2监听不到home键

上篇文章总结了下home键监听的4种方法,链接如下:

Android中监听Home键的4种方法

 

经测试发现,在魔百盒CM201-2上通过广播不能实现home键监听。遂,,记录下当前解决方法:

采用第二种方法可以解决当前尴尬问题,代码如下:

    /**
     * 兼容移动魔百盒CM201-2
     */
    @Override
    protected void onUserLeaveHint() {
        Log.d(TAG,"短按home键...");
        Log.d(TAG,"onUserLeaveHint...");
        super.onUserLeaveHint();
        Intent intent = new Intent(MainActivity.this, BackgroundService.class);
        intent.setAction(STOP_SERVICE);
        startService(intent);
    }

 

但是如果从一个Activity启动一个新的Activity,onUserLeaveHint也会被调用。。。

再次翻阅文档,发现Intent中的一个Flag:
 

public static final int FLAG_ACTIVITY_NO_USER_ACTION

Since: API Level 3
If set, this flag will prevent the normal onUserLeaveHint() callback from occurring on the current frontmost activity before it is paused as the newly-started activity is brought to the front.

Typically, an activity can rely on that callback to indicate that an explicit user action has caused their activity to be moved out of the foreground. 
The callback marks an appropriate point in the activity's lifecycle for it to dismiss any notifications that it intends to display "until the user has seen them," such as a blinking LED.
If an activity is ever started via any non-user-driven events such as phone-call receipt or an alarm handler, this flag should be passed to Context.startActivity, ensuring that the pausing activity does not think the user has acknowledged its notification.


这正是我想要的,这样,在启动activity时,往intent中加上这个flag,onUserLeaveHint就不会再被调用了,hoory...

你可能感兴趣的:(技术总结,android)