如何让广播在Android的锁屏Android

2023-09-07 02:47:01 作者:借风抱你

如何获得触发屏幕在Android是锁定或在?我试着用SCREEN_OFF&安培; SCREEN_ON行动广播接收器,但它不工作。

 公共无效的onReceive(上下文的背景下,意图意图){        Log.d(XYZ,画面ON / OFF);        Toast.makeText(背景下,银幕,10000).show();        如果(intent.getAction()。等于(Intent.ACTION_SCREEN_ON))        {.......}} 

在活动我已经注册像广播 -

屏幕是我的广播接收机的对象

  IntentFilter的过滤器=新的IntentFilter(Intent.ACTION_SCREEN_ON);            filter.addAction(Intent.ACTION_SCREEN_OFF);            mContext.registerReceiver(屏幕,过滤器); 

解决方案 系统 Android4.0全新界面

调用 UpdateService.class 中的 MainActivity.class

startService(新意图(MainActivity.this,UpdateService.class));

UpdateService.class

 公共类UpdateService延伸服务{    广播接收器mReceiver;    公共静态INT countOn = 0;    公共静态INT countOff = 0;    @覆盖    公共无效的onCreate(){        super.onCreate();        //注册接收处理屏幕上,屏幕关闭逻辑        Log.i(UpdateService,开始);        IntentFilter的过滤器=新的IntentFilter(Intent.ACTION_SCREEN_ON);        filter.addAction(Intent.ACTION_SCREEN_OFF);        filter.addAction(Intent.ACTION_ANSWER);        mReceiver =新MyReceiver();        registerReceiver(mReceiver,过滤器);    }    @覆盖    公共无效的onDestroy(){        unregisterReceiver(mReceiver);        Log.i(的onDestroy Reciever,被称为);        super.onDestroy();    }    @覆盖    公众诠释onStartCommand(意向意图,诠释标志诠释startId){        // TODO自动生成方法存根        布尔screenOn = intent.getBooleanExtra(screen_state,FALSE);        如果(!screenOn){            Log.i(screenON,被称为);            Log.i(viaService,CountOn =+ countOn);            Toast.makeText(getApplicationContext(),醒,Toast.LENGTH_LONG)                    。显示();        }其他{            Log.i(SCREENOFF,被称为);            Log.i(viaService,CountOff =+ countOff);        }        返回START_STICKY;    }    @覆盖    公众的IBinder onBind(意向意图){        // TODO自动生成方法存根        返回null;    }} 

Receiver类

 公共类MyReceiver扩展广播接收器{    私人布尔SCREENOFF;    @覆盖    公共无效的onReceive(上下文的背景下,意图意图){        如果(intent.getAction()。等于(Intent.ACTION_SCREEN_OFF)){            SCREENOFF = TRUE;            // Log.i(通过接收器,正常SCREENOFF);        }否则如果(intent.getAction()。等于(Intent.ACTION_SCREEN_ON)){            SCREENOFF = FALSE;        }否则如果(intent.getAction()。等于(Intent.ACTION_ANSWER)){        }        意图I =新意图(背景下,UpdateService.class);        i.putExtra(screen_state,SCREENOFF);        context.startService(ⅰ);    }} 

How to get trigger that screen is locked or on in android?? i tried using SCREEN_OFF & SCREEN_ON action in broadcast receiver but it's not working.

public void onReceive(Context context, Intent intent) {
        Log.d("XYZ", "Screen ON/OFF");

        Toast.makeText(context, "screen",10000).show();
        if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))
        {
.......
}
}

in activity i have registered broadcast like-

screen is object of my broadcast receiver

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            mContext.registerReceiver(screen, filter);

解决方案

Call the UpdateService.class within your MainActivity.class .

startService(new Intent(MainActivity.this, UpdateService.class));

UpdateService.class

public class UpdateService extends Service {

    BroadcastReceiver mReceiver;
    public static int countOn = 0;
    public static int countOff = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        // register receiver that handles screen on and screen off logic
        Log.i("UpdateService", "Started");
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_ANSWER);
        mReceiver = new MyReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onDestroy() {

        unregisterReceiver(mReceiver);
        Log.i("onDestroy Reciever", "Called");

        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
            Log.i("screenON", "Called");
            Log.i("viaService", "CountOn =" + countOn);

            Toast.makeText(getApplicationContext(), "Awake", Toast.LENGTH_LONG)
                    .show();
        } else {
            Log.i("screenOFF", "Called");
            Log.i("viaService", "CountOff =" + countOff);
        }


        return START_STICKY;
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

Receiver class

public class MyReceiver extends BroadcastReceiver {
    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
            // Log.i("via Receiver","Normal ScreenOFF" );
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
        } else if(intent.getAction().equals(Intent.ACTION_ANSWER)) {

        }

        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }

}