Search code examples
androidbroadcastreceiver

Why my BroadcastReceiver doesn't work?


I'm trying to learn to use Android BroadcasReceiver. I wrote this code but It doesn't work... I tryed for example to change the Time etc... What is it wrong?

I added in the Manifest:

<receiver android:name="com.example.broadcastreceiverspike.Broadcast" >
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.ACTION_SCREEN_ON" />
            <action android:name="android.intent.action.ACTION_SCREEN_OFF" />
            <action android:name="android.intent.action.TIME_SET" />
        </intent-filter>
</receiver>

My simple BroadcasReceiver:

    public class Broadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
           Log.d("BROADCAST", "It worked");
           Toast.makeText(context, "BROADCAST",  Toast.LENGTH_LONG).show();
        }
}

My Main Activity (default main Activity)

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Solution

  • I solved!

    "unlike other broad casted intents, for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON you CANNOT declare them in your Android Manifest! I’m not sure exactly why, but they must be registered in an IntentFilter in your JAVA code"

    http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

    Manifest

        <receiver android:name=".Broadcast" >
            <intent-filter>                
                <action android:name="android.intent.action.TIME_SET" />
    
            </intent-filter>
        </receiver>
    

    Main Activity Class

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
         // INITIALIZE RECEIVER
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new Broadcast();
        registerReceiver(mReceiver, filter);
    
    }
    

    }

    My broadcast receiver

    public class Broadcast extends BroadcastReceiver {

    public static String TAG = "BROADCAST";
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        if (intent.getAction().equals(Intent.ACTION_TIME_CHANGED))
        {
            Log.d(TAG, "BROADCAST Cambio di orario");
            Toast.makeText(context, "BROADCAST Cambio di orario", Toast.LENGTH_LONG).show();
        }
        else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            // DO WHATEVER YOU NEED TO DO HERE
            Log.d(TAG, "BROADCAST Screen OFF");
            Toast.makeText(context, "Screen OFF", Toast.LENGTH_LONG).show();
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // AND DO WHATEVER YOU NEED TO DO HERE
            Log.d(TAG, "BROADCAST Screen ON");
            Toast.makeText(context, "BROADCAST Screen ON", Toast.LENGTH_LONG).show();
        }
    }
    

    }