0

I found this code on internet and I don't know how to get permission for this Broadcast Receiver..

or I don't need to?

It's in a class SetAlarmActivity..

BroadcastReciever br;
private void setup() {
        br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context c, Intent i) {
            Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
        }
    };
    registerReceiver(br, new IntentFilter("org.example.makwanas.sapalarm") );
    pi = PendingIntent.getBroadcast( this, 0, new Intent("org.example.makwanas.sapalarm"),
            0 );
    am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Makwana
  • 70
  • 10
  • 1
    You already *have registered* the receiver in code using `registerReceiver`, so you do not need to *register* again in Manifest file. Permission is different. – MysticMagicϡ Oct 27 '14 at 07:22
  • Thanks ,, Ok one more Doubt what should be there in registerReceiver(br, new IntentFilter("org.example.makwanas.sapalarm") ); at the place new IntentFilter("??"); a packege or a class?? or what exactly – Makwana Oct 27 '14 at 07:33

2 Answers2

1
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntentFilter filter = new IntentFilter();
        filter.addAction("Some_Broadcast");
        registerReceiver(reciever, filter);
    }

  @Override
    protected void onDestroy() {
        if (reciever != null) {
            unregisterReceiver(reciever);
            reciever = null;
        }
        super.onDestroy();
    }
Ankit Tomer
  • 127
  • 1
  • 4
0

You already have registered the receiver in code using registerReceiver, so you do not need to register again in Manifest file. Permission is different.

Refer documentation of registerReceiver

filter - Selects the Intent broadcasts to be received.

So you will need to give intent filter for which your receiver needs to receive broadcasts.

Sample:

IntentFilter filter = new IntentFilter();
filter.addAction("SOME_ACTION");
filter.addCategory("SOME_CATEGORY"); 

You can also see this blog for more detailed explanation and examples.

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • Thank You Very Much.. But i Have added intent filter in a diffrent way.. like registerReceiver(br, new IntentFilter("org.example.makwanas.sapalarms") ); so what should be there in " " , a packege name or name of the class or what ?? like i have given now org.example.makwanas.sapalarm – Makwana Oct 27 '14 at 09:08
  • okay so for alarm clock application i have added action as ALARM_SERVICE and catagory also ALARM_SERVICE ?? as you suggested me to do,, is that fine – Makwana Oct 27 '14 at 09:16
  • That should be intent filter for which your receiver needs to receive broadcasts. Example: for receiving broadcast of device booted, you would need to add `Intent.ACTION_BOOT_COMPLETED`. See [this](http://www.techotopia.com/index.php/Android_Broadcast_Intents_and_Broadcast_Receivers) and [this](http://www.compiletimeerror.com/2013/03/android-broadcast-receiver-in-detail.html#.VE4Nhxblu1s) @Makwana – MysticMagicϡ Oct 27 '14 at 09:16
  • @Makwana check [this](http://stackoverflow.com/a/7061752/1777090) for alarm + broadcast receiver. – MysticMagicϡ Oct 27 '14 at 09:19
  • Thanks Dude !! I understood how to deal with it.. and its working :) – Makwana Oct 27 '14 at 10:40