android - 屏幕锁定和解锁

标签 android

我想知道当用户在 android 中锁定手机时屏幕是关闭的。我像这样使用 BroadCast 接收器并注册了但我不知道屏幕是否关闭

public class MyActivity extends Activity{   

    private static final String ACTION = "android.intent.action.ACTION_SCREEN_OFF";
    BroadcastReceiver myReceiver;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        context = this;
        final IntentFilter theFilter = new IntentFilter();
        theFilter.addAction(ACTION);

        context.registerReceiver(myReceiver, theFilter);
        System.out.println("inside increate");
        myReceiver = new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub      
                    System.out.println("phone locked*****");                
            }

        };   

    }}

如何找出这个问题?

最佳答案

第 1 步:删除 private static final String ACTION

第 2 步:在 addAction() 行中使用 Intent.ACTION_SCREEN_OFF(或者,更好的是,将其提供给 IntentFilter构造函数并去掉 addAction() 行)

第 3 步:使用 android.util.Log 代替 System.out.println()

关于android - 屏幕锁定和解锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9004394/

相关文章:

android - Eclipse ADT facebook 示例应用程序出现错误

android - 控制安卓或任何其他音乐播放器的默认音乐播放器

java - 具有自定义布局的 ListView,包括两个 RadioButton

android - 插入或更新的 Realm 顺序

java - 删除应用程序时是否删除与应用程序关联的共享首选项?

android - android.permission.RECEIVE_BOOT_COMPLETED 不是必需的吗?

android - 禁用 ListView 中的滚动

java - Jackson:将 JSON 转换为对象:ArrayList of objects with arraylist of objects with arraylist of objects

android - 如何从服务器读取pdf内容并保存在文件中?

java - 使用 Context.getApplicationContext() 进行 Android JUnit 测试