android-studio - Android中如何判断当前屏幕的状态?
黄舟
黄舟 2017-04-17 16:13:26
0
2
492

我想过监听系统广播,可是我在manifest.xml中声明了想要监听的广播类型,然后重写了onReceive()方法,以打印Log的方式检测是否正常工作,然而在控制台发现没有打印出信息。直接上图吧:


黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
小葫芦

Brother, under what circumstances do you want to monitor the screen status? ?

1. If you want to monitor all the time, you have to enable the service

Configuration in Manifest

        <!--广播接收者-->
        <receiver android:name=".receiver.ScreenStatusReceiver">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_OFF" />
                <action android:name="android.intent.action.SCREEN_ON" />
            </intent-filter>
        </receiver>

        <!--服务-->
        <service android:name=".service.PoService"/>

Service

public class PoService extends Service{
    int mStartMode;       // indicates how to behave if the service is killed
    IBinder mBinder;      // interface for clients that bind
    boolean mAllowRebind; // indicates whether onRebind should be used
    private ScreenStatusReceiver mReceiver;

    @Override
    public void onCreate() {
        final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);

        mReceiver = new ScreenStatusReceiver();
        registerReceiver(mReceiver, filter);
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // The service is starting, due to a call to startService()
        return mStartMode;
    }
    @Override
    public IBinder onBind(Intent intent) {
        // A client is binding to the service with bindService()
        return mBinder;
    }
    @Override
    public boolean onUnbind(Intent intent) {
        // All clients have unbound with unbindService()
        return mAllowRebind;
    }
    @Override
    public void onRebind(Intent intent) {
        // A client is binding to the service with bindService(),
        // after onUnbind() has already been called
    }
    @Override
    public void onDestroy() {
        // The service is no longer used and is being destroyed
        if (mReceiver != null) {
            unregisterReceiver(mReceiver);
            mReceiver = null;
        }
    }
}

BroadcastReceiver

public class ScreenStatusReceiver extends BroadcastReceiver {
    public static boolean wasScreenOn = true;

    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            wasScreenOn = false;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            wasScreenOn = true;
        }
    }
}

Log

03-16 21:57:22.950 30953-30953/wang.wangxinarhat.materialdesigncomprehensivesamples E/ScreenStatusReceiver: 屏幕关闭
03-16 21:57:23.979 30953-30953/wang.wangxinarhat.materialdesigncomprehensivesamples E/ScreenStatusReceiver: 屏幕点亮
03-16 21:57:26.584 30953-30953/wang.wangxinarhat.materialdesigncomprehensivesamples E/ScreenStatusReceiver: 屏幕关闭
03-16 21:57:28.485 30953-30953/wang.wangxinarhat.materialdesigncomprehensivesamples E/ScreenStatusReceiver: 屏幕点亮
03-16 21:57:31.021 30953-30953/wang.wangxinarhat.materialdesigncomprehensivesamples E/ScreenStatusReceiver: 屏幕关闭
03-16 21:57:32.393 30953-30953/wang.wangxinarhat.materialdesigncomprehensivesamples E/ScreenStatusReceiver: 屏幕点亮

2. If not, let me give you an example of getting the screen status in Activity

Activity

public class ScreenStatusActivity extends BaseActivity {
    private BroadcastReceiver mReceiver = null;

    public static Intent getStartIntent() {
        Intent intent = new Intent();
        intent.setClass(BaseApplication.getApplication(), ScreenStatusActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);

        mReceiver = new ScreenStatusReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onPause() {

        if (ScreenStatusReceiver.wasScreenOn) {

            Log.e(ScreenStatusActivity.class.getSimpleName(), "屏幕点亮");
        } else {
            Log.e(ScreenStatusActivity.class.getSimpleName(), "屏幕关闭");
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (ScreenStatusReceiver.wasScreenOn) {
            Log.e(ScreenStatusActivity.class.getSimpleName(), "屏幕点亮");
        } else {
            Log.e(ScreenStatusActivity.class.getSimpleName(), "屏幕关闭");
        }
    }

    @Override
    protected void onDestroy() {
        if (mReceiver != null) {
            unregisterReceiver(mReceiver);
            mReceiver = null;
        }
        super.onDestroy();
    }

}

Log

03-16 21:14:03.112 3803-3803/wang.wangxinarhat.materialdesigncomprehensivesamples E/ScreenStatusActivity: 屏幕点亮
03-16 21:14:09.583 3803-3803/wang.wangxinarhat.materialdesigncomprehensivesamples E/ScreenStatusActivity: 屏幕关闭
03-16 21:14:26.686 3803-3803/wang.wangxinarhat.materialdesigncomprehensivesamples E/ScreenStatusActivity: 屏幕点亮
03-16 21:14:41.576 3803-3803/wang.wangxinarhat.materialdesigncomprehensivesamples E/ScreenStatusActivity: 屏幕关闭

This part of the code is placed on github

左手右手慢动作

The use of broadcast in Service is the same as that in Activity. Just register it in onCreate and unbind it in onDestroy. It's just that these two broadcasts are special. They cannot be registered statically, but can only be registered dynamically. That is, it will not work if you register this broadcast monitoring in the manifest. This is why the demo you wrote yourself cannot print out the log. Intent.ACTION_SCREEN_ONIntent.ACTION_SCREEN_OFF

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!