BroadcastReceiver for Screen On/Off not working

后端 未结 3 1380
夕颜
夕颜 2020-12-05 21:43

I am trying to use BroadcastReceiver but it is not working, please help me to solve this problem. MyReceiver.java

package com.example.broadcast_receiver;

im         


        
相关标签:
3条回答
  • 2020-12-05 22:00

    If you want this receiver to be called by the system, you would need to export it. You set exported = "false", change this to true or remove exported entirely and this will start working. Normally this would be insecure, but as both SCREEN_ON and SCREEN_OFF are protected-broadcasts, and you verify the actions, only more trusted system code can send them too you, so it's pretty safe.

    Sadly this wont work in this case as the intents broadcast have the following flags: Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND

    0 讨论(0)
  • 2020-12-05 22:05

    Can you try with getting battery value:

    public class Broadcast 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) {
                //Get battery percentage
                int batterylevel = intent.getIntExtra("level", 0);    
                //get progressbar
                ProgressBar myprogressbar = (ProgressBar) findViewById(R.id.progressbar);
                myprogressbar.setProgress(batterylevel);
                TextView tv = (TextView) findViewById(R.id.textfield);
                //Set TextView with text
                tv.setText("Battery Level: " + Integer.toString(batterylevel) + "%");
            }
        });
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_broadcast);   
            registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        }
    } 
    
    0 讨论(0)
  • 2020-12-05 22:08

    Hey try using dynamic calling of broadcast,I tried this it will surly work...

    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));
        }
    }
    
    0 讨论(0)
提交回复
热议问题