簡體   English   中英

如何在android中獲得屏幕鎖定廣播

[英]How to get broadcast for screen lock in android

如何在android中觸發屏幕被鎖定或打開?

我嘗試在廣播接收器中使用 SCREEN_OFF 和 SCREEN_ON 操作,但它不起作用。

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)) {
        .......
    }
}

在活動中,我注冊了廣播,例如-

屏幕是我的廣播接收器的對象

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

MainActivity.class調用UpdateService.class

startService(new Intent(MainActivity.this, 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;
    }
}

接收器類

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);
    }

}

嘿嘗試使用廣播的動態調用,我試過這個它會很好地工作......

public class MainActivity extends Activity {
     //Create broadcast object
       BroadcastReceiver mybroadcast = new BroadcastReceiver() {

        //When Event is published, onReceive method is called
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
                Log.i("[BroadcastReceiver]", "MyReceiver");

                if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
                    Log.i("[BroadcastReceiver]", "Screen ON");
                }
                else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
                    Log.i("[BroadcastReceiver]", "Screen OFF");
                }

        }
    };

     @Override
       public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
       registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
     }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM