android判断屏幕状态错误,android-studio - Android中如何判断当前屏幕的状态?

小葫芦2017-04-17 16:15:262楼

哥们你想在什么情况下监听屏幕状态啊??

1.如果是要一直监听就得开启服务

Manifest中配置

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;

}

}

}

日志

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.如果不是,给你举个在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();

}

}

日志

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: 屏幕关闭

这部分代码放到了github

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值