我怎么知道Android手机什么时候入睡?

时间:2010-09-19 07:30:40

标签: android

我怎么知道Android手机什么时候入睡?我尝试使用示例代码但没有发生任何事情。

这是我到目前为止所做的:

ScreenON_OFF_ACTIVITY.java

package com.pack;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class ScreenON_OFF_ACTIVITY extends Activity {

     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            onCreate();
     }
     public void onCreate() {

            // initialize receiver
            System.out.println("onCreate1 ");
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            BroadcastReceiver mReceiver = new ScreenReceiver();
            registerReceiver(mReceiver, filter);
            System.out.println("onCreate ");        
        }

        @Override
        protected void onPause() {
            // when the screen is about to turn off
            if (ScreenReceiver.screenOff) {
                // this is the case when onPause() is called by the system due to a screen state change
                System.out.println("SCREEN TURNED OFF");
            } else {
                // this is when onPause() is called when the screen state has not changed
                 System.out.println("this is when onPause() is called when the screen state has not changed ");

            }
            super.onPause();
        }

        @Override
        protected void onResume() {
            // only when screen turns on
            if (!ScreenReceiver.screenOff) {
                // this is when onResume() is called due to a screen state change
                System.out.println("SCREEN TURNED ON");
            } else {
                // this is when onResume() is called when the screen state has not changed
                 System.out.println(" this is when onResume() is called when the screen state has not changed ");
            }
            super.onResume();
        }



    }

ScreenReceiver.java

package com.pack;

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

public class ScreenReceiver extends BroadcastReceiver {

    public static boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {

         System.out.println("onReceive ");
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
            System.out.println("SCREEN TURNED OFF on BroadcastReceiver");
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
            System.out.println("SCREEN TURNED ON on BroadcastReceiver");
        }
        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }

}

UpdateService.java

package com.pack;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

public class UpdateService extends Service {

    public void onCreate() {
        super.onCreate();
        // register receiver that handles screen on and screen off logic
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
            System.out.println("Screen is off");
        } else {
            System.out.println("Screen is on");
        }
    }

    @Override
    public IBinder onBind(Intent intent) {      
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

根据我的知识你不知道Android手机是否会睡觉。只有一件事你可以保证在重新唤醒后,随时可以使用安卓手机。 屏幕关闭后可能会发生这种情况(屏幕黑色)。