nilkash
nilkash

Reputation: 7536

Broadcast receiver works only when device reboot android

Hi I am developing android application in which I am defining one broadcast receiver.I am calling receiver from my activity. I am defining broadcast receiver like this :

 public class MyScheduleReceiver extends BroadcastReceiver {

   private static final long REPEAT_TIME = 100 * 5;

  @Override
  public void onReceive(Context context, Intent intent) {
      Log.i("RRRRRRRRRRRRRRRRRRRRRRRR", "on receive");

  }
} 

In android manifest file I am defining like this:

 <receiver android:name="abc.xyz.MyScheduleReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver> 

and in main activity I am calling my broadcast receiver like this :

//in activity oncreate 
startService(new Intent(this, MyScheduleReceiver.class));

My problem is that when call start service it's not starting my service actually. But when i restart my device it start my service because I gave intent filter "BOOT_COMPLETED". what I wanted to do actually when i call start service my service must be start,

Am I doing something wrong. How to solve this problem?

Upvotes: 0

Views: 1319

Answers (4)

Atif Farrukh
Atif Farrukh

Reputation: 2261

to start broadcasts you need to send broadcasts not startService()

add this instead of startService(new Intent(this, MyScheduleReceiver.class));

Intent intent = new Intent();
intent.setAction("pakagename.MyScheduleReceiver");
sendBroadcast(intent); 

I hope it helps.

Upvotes: 0

Sreedev
Sreedev

Reputation: 6653

Actual what happens here is that you can staring a broadcast receiver while starting the activity and this broadcast receiver starts listening BOOT_COMPLEATED is happening or not. When this happens it comes to onreceive . If you need to start a process doing in background you can use a a Service insted of BroadcastReciever. BroadcastRecievers are used to listen for some events to happen.Go through this, it will help you

Services

BroadcastReceiver

Upvotes: 3

Kumar Bibek
Kumar Bibek

Reputation: 9127

startService call would only start a Service. MyScheduleReceiver here is a braodcast receiver. To trigger broadcast receivers, you generally have to send broadcasts and not call the startService.

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93708

You're either confused, or you aren't wording your question well. What you have in your manifest (and how Android works generally) is that when BOOT_COMPLETED occurs, it will call that BroadcastReceiver you defined. It will not automatically start an activity or service. If you want to do that, you need to call startService or startActivity in your onReceive function of the receiver.

You do not start BroadcastReceivers. You start services, which are long term background processes. You register BroadcastReceivers to be informed of special events (like BOOT_COMPLETED). When one of the events you registered for occurs, it will create an instance of that class and call its onReceive.

Hopefully that clears things up. If not, I suggest you reread some tutorials on services and broadcast receivers, you seem to have the two confused.

Upvotes: 1

Related Questions