应用终止时检测手机是否唤醒。 -安卓

时间:2018-07-20 08:24:11

标签: android service android-service

我正在尝试检测手机的唤醒情况。当应用在后台或前台运行时,我设法做到了。但是,在应用终止时,我需要能够做到这一点。

在我的代码中,我创建了一个新服务,并在onCreate方法中启动了它。这是我的代码:

主要活动类别:

public class MainActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       startService(new Intent(this, ScreenService.class));
   }
}

我的服务班级:

public class ScreenService extends Service{

    private BroadcastReceiver sReceiver;

    public IBinder onBind(Intent arg){
       return null;
    }

    public  int onStartCommand(Intent intent,int flag, int startIs) {

        IntentFilter filter=new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        sReceiver=new ScreenReceiver();
        registerReceiver(sReceiver,filter);
        return  START_STICKY;

   }
}

还有我的服务处理程序类:

public class ScreenReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent){

    if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
        Log.e("Log", "The screen is on.");
    }
    else{
        Log.e("Log", "The screen is off.");
    }
  }
}

应用终止后,我该如何检测电话唤醒?

0 个答案:

没有答案