激活锁定只能连接USB电缆工作 [英] Wakelock only working with usb-cable connected

查看:257
本文介绍了激活锁定只能连接USB电缆工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当奇怪的问题。最近我尝试使用以下code用在我的应用程序激活锁定:

I have a rather odd problem. Lately i tried to use a wakelock in my application using the following code:

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    screenLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
    button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            System.out.println("Clicked");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (!locked)
                setKeepScreenOn(WakeupLightActivity.this, true);
            else setKeepScreenOn(WakeupLightActivity.this, false);
            System.out.println("Screen will stay on");
        }
    });
}

public void setKeepScreenOn(Activity activity, boolean keepScreenOn) {
    if(keepScreenOn) {                    
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);          
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        this.screenLock.acquire();
    } 
    else {                                 
       activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);             
       activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        this.screenLock.release();
    }
}

在激活锁定完美的作品:5秒后pressing按钮(和锁定我的电话)屏幕打开。这个测试,但是,做而我的手机连接到我的笔记本电脑使用USB电缆调试。

The wakelock works perfectly: 5 seconds after pressing the button (and locking my phone) the screen turns on. This test, however, was done while my phone was connected to my laptop for debugging using a USB cable.

现在来了怪异的一部分。当我没有我的手机用USB电缆连接时,只需激活锁定不转在屏​​幕上。我使用的设备是HTC渴望。其实我已经在另一个HTC渴望,它也做了同样的事情进行了测试。接下来,我试了一下上的HTC Desire HD。并猜测:它再完美!我真搞不清楚这一点,并想知道如果问题仅仅是一个缺陷在我的手机类型,或者如果我做错了什么在我的code。有什么我可以做些什么?这将是令人难以置信的尴尬后,如果已经购买我的应用程序的任何潜在客户提供了手机故障类型都会遇到同样的问题。

Now comes the weird part. When I DON'T have my phone connected with the USB cable, the wakelock simply DOES NOT turn on the screen. The device I am using is an HTC desire. I have actually tested it on another HTC desire, which did the same thing. Next, i tried it on an HTC Desire HD. And guess what: It worked perfectly again! I am really confused about this and wondering if the problem is simply a flaw in my phone type, or if I am doing something wrong in my code. Is there anything I can do about it? It would be incredibly awkward if any potential customers with a malfunctioning phone type would encounter the same problem after having purchased my app.

请注意,我也尝试应用在充电时我的手机连接到墙壁插头的电缆。这导致了相同的问题,因为不具有我的电话连接的。这两种欲望HTC运行相同的Andr​​oid版本(2.2.2)。

Note that I also tried the application while charging my phone with the cable connected to a wall plug. This leads to the same problem as not having my phone connected at all. Both HTC Desires run the same Android version (2.2.2).

推荐答案

显然,这一切的发生仅仅是因为我使用了错误的标志。该问题是由具有以下标志创建激活锁定解决:

Apparently all of this occured simply because i was using the wrong flags. The problem was solved by creating the WakeLock with the following flags:

PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP

这篇关于激活锁定只能连接USB电缆工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆