开发者

How can I know when an Android phone is going to sleep?

开发者 https://www.devze.com 2023-01-16 14:05 出处:网络
How can I know when an Android phone is going to sleep? I tried with the sample code but nothing is happening.

How can I know when an Android phone is going to sleep? I tried with the sample code but nothing is happening.

Here is what I have so far:

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();
            register开发者_StackOverflow社区Receiver(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;
    }
}


According to my knowledge you can not know Android Phone is going to sleep or not.only one thing you can guarantee after relase wakelock android phone can be going sleep any time. It can happen after screen off (screen black).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号