7

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;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {

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

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcast_receiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name=".MyReceiver" 
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_ON"/>
                <action android:name="android.intent.action.SCREEN_OFF"/>
            </intent-filter>
        </receiver>

        <activity
            android:name="com.example.broadcast_receiver.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

BroadcastReceiver not working and not making any log, please help me to solve this problem.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
user2290872
  • 95
  • 1
  • 1
  • 5
  • Check http://stackoverflow.com/a/9478013/1777090 – MysticMagicϡ Apr 23 '13 at 04:40
  • Please Use any Other Service to Regitser. Message Recieved or Incoming Call. Please check the LOg. – Akshay Joy Apr 23 '13 at 04:42
  • check this one might help full for you http://stackoverflow.com/questions/9477922/android-broadcast-receiver-for-screen-on-and-screen-off – nilkash Apr 23 '13 at 04:52
  • @user2290872 Please Check this, it might be helpful [Screen OFF/ON broadcast listener without service](https://stackoverflow.com/questions/48598200/screen-on-off-broadcast-listener-for-a-widget-on-android-oreo/52853205#52853205) – Ankit Kumar Singh Oct 23 '18 at 10:49

3 Answers3

23

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));
    }
}
Antoine
  • 3,880
  • 2
  • 26
  • 44
kavya
  • 468
  • 2
  • 4
  • 10
  • 2
    yaa it is working, bt i want that it is work after closing my app bcoz I m making lock screen, so u have any idea for that ? – user2290872 Apr 23 '13 at 05:45
  • you call this broadcast inside "services" – kavya Apr 23 '13 at 08:09
  • kk thnx i did it, but whn i closed application n try to screen off n on thn it is not working so i check running services on phone n show that there is no service that i made, so can u help 4 that ? – user2290872 Apr 23 '13 at 12:09
  • kk thnx now service is working in background bt i want to open activity whn screen on, bt id that activity is already running thn it can not run again n show old open activity, how can i do dis can u help me 4 dis ? – user2290872 Apr 23 '13 at 12:43
  • my app do not contains any services – Prasad Jul 31 '15 at 12:26
  • Because nobody mentioned it: You can't register receivers for screen on/off actions in the manifest. These ones only work when registered programmatically. – JacksOnF1re May 24 '16 at 12:32
  • it is possible to add multiple actions to one intent filter – Jacques Giraudel Nov 23 '17 at 14:01
  • Please Check this [Screen OFF/ON broadcast listener without service](https://stackoverflow.com/questions/48598200/screen-on-off-broadcast-listener-for-a-widget-on-android-oreo/52853205#52853205) – Ankit Kumar Singh Oct 17 '18 at 13:07
  • how can I share a variable with IntentFilter like putextra in intent? – Wasim A. May 03 '20 at 23:11
5

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

Jesse
  • 51
  • 1
  • 2
3

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));
    }
} 
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
kavya
  • 468
  • 2
  • 4
  • 10