检测屏幕原因

时间:2018-07-25 08:02:59

标签: java android android-service

我有一个应用可以在ACTION_SCREEN_ON上播放视频。但是,如果屏幕上的原因是来电,我不想播放视频。我试图用TelephonyManager.ACTION_PHONE_STATE_CHANGED检测传入的电话,但无法。如何做到这一点?您可以在下面找到我的代码。

Services.java

public class Services 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);
        //TelephonyManager.ACTION_PHONE_STATE_CHANGED ????
        sReceiver=new Receivers();
        registerReceiver(sReceiver,filter);
        return  START_STICKY;

    }
}

Receivers.java

public class Receivers extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent){

        if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
            Log.e("Log", "The screen is on.");
            Intent i = new Intent(context.getApplicationContext(), CommercialVideo.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
        else{
            Log.e("Log", "The screen is off.");
        }
    }
}

0 个答案:

没有答案