ACTION_SCREEN__ON,ACTION_SCREEN__OFF不工作? [英] ACTION_SCREEN__ON,ACTION_SCREEN__OFF not working?

查看:144
本文介绍了ACTION_SCREEN__ON,ACTION_SCREEN__OFF不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试打开wifi关闭时,画面关闭(上锁) 并开启无线网络时再屏幕上

im trying to turn wifi off when screen OFF(locked) and turn wifi on again when screen ON

我犯了一个BroadcastReceiver 我把mainfest这个code:

i made a broadcastReceiver i put in mainfest this code:

<receiver android:name="MyIntentReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.SCREEN_OFF" />
                <action android:name="android.intent.action.SCREEN_ON" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.LAUNCHER" />    
            </intent-filter>
        </receiver>

和这是类MyIntentReceiver

and this is the class MyIntentReceiver

package org.androidpeople.boot;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyIntentReceiver extends BroadcastReceiver {
    // Called when boot completes

    public static boolean startup;

    @Override
    public void onReceive(Context context, Intent intent) {
        // Set what activity should launch after boot completes

        System.out.println("Intent Action: " + intent.getAction());

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

            System.out.println("locked : ACTION_SCREEN_OFF");

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

            System.out.println("not locked : ACTION_SCREEN_ON ");

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

            System.out.println("User Unlocking it ");

        } 
        else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            // this to indicate that program is running
            // automaticlly not manually by user
            startup = true;
            System.out.println("Automatic BOOT at StartUp");

            Intent startupBootIntent = new Intent(context, LaunchActivity.class);
            startupBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(startupBootIntent);
        }

    }
}

所以resault是ACTION_SCREEN_ON ACTION_SCREEN_OFF从来没有发射!!!! USER_ preSENT和BOOT_COMPLETED都工作得很好,但对方没有工作!

so the resault is ACTION_SCREEN_ON ACTION_SCREEN_OFF never fired!!!! USER_PRESENT and BOOT_COMPLETED are worked Fine but the other not working!!!

即时通讯使用仿真器不雷尔设备,所以我不知道这是什么问题?!

IM Using Emulator not reall device so i dont know if this is the problem?!

任何帮助??? 我需要赶在屏幕上和关闭,以启用/禁用无线网络 保存battary

any help??? i need to catch the screen on and off in order to enable/disable wifi to save battary

在此先感谢

推荐答案

通过XML无法捕捉那些意图(我忘了为什么)。但是,您可以使用服务是注册在的BroadcastReceiver 成员公司 onStartCommand()并注销在其的onDestroy()。这将需要该服务会在后台运行,不断地或者只要你需要它,所以一定要探索替代路线。

You cannot catch those intents through XML (I forget why). However, you could use a Service that registers a BroadcastReceiver member in its onStartCommand() and unregisters it in its onDestroy(). This would require the service to be running in the background, constantly or as long as you need it to, so be sure to explore alternative routes.

您可以定义的BroadcastReceiver 服务类,像这样:

You could define the BroadcastReceiver in your Service class like so:

private final class ScreenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
             //stuff
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
             //other stuff
        }
    }
}

对于一个稍微复杂的例子,而是一个显示了的BroadcastReceiver 服务互动看<一href="http://$c$c.google.com/p/electricsleep/source/browse/trunk/src/com/androsz/electricsleepbeta/app/CheckForScreenBugAccelerometerService.java">CheckForScreenBugAccelerometerService从我的应用程序,ElectricSleep。

For a slightly complicated example, but one that shows how the BroadcastReceiver and Service interact see CheckForScreenBugAccelerometerService from my app, ElectricSleep.

这篇关于ACTION_SCREEN__ON,ACTION_SCREEN__OFF不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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