我有两个类,它们是 MainActivity 和 MyBroadcastReceiver。BroadcastReceiver 检测手机屏幕是打开还是关闭。我的愿望是在屏幕锁定释放时启动我的应用程序。我的意思是我想在电话锁释放时将我的应用程序放在前面。
这是我的活动课:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver();
}
private void registerReceiver(){
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new MyPhoneReceiver();
registerReceiver(mReceiver, filter);
}
}
这是我的广播接收器:
public class MyPhoneReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if(pm.isScreenOn()){
//Bring application front
}
}
}
为了在广播接收器中执行此操作,我应该怎么做?