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...

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值