android - service and broadcastreceiver -


I am trying to create a service in which I am calling a method repeatedly after 5000ms. This method enhances a counter and when it reaches 6, I want to call other activity through a broadcast receiver. But I'm getting an error "Do you remember calls to unregister ()?" Can anyone tell me what is the problem really?

Main activity

  Public class services implements activity activity activity OnClickListener {button start, stop; IntentFilter intentfilter; @ Override Protected Zero (Bundle Saved Instantstate) {Super. NET (Saved Instantstate); SetContentView (R.layout.activity_services_example); Intentfilter = new intent filter (); Intentfilter.addAction ("LOCATION_REACHED"); Register receiver (interventor, intent filter); Start = (button) findViewById (R.id.start); Stop = (button) VVBIID (R.Stop); Start.setOnClickListener (this); Stop.setOnClickListener (this); } @Override Public Zero onClick (see v) {if (v.getId () == R.id.start) {startService (getApplicationContext (), ServiceClass.class); } And if (v.getId () == rdstop) {stopService (new intent (getApplicationContext (), ServiceClass.class)); }} Private BroadcastReceiver intentreceiver = new BroadcastReceiver () {@Override public void onReceive (Context context, Intent intent) {if (intent.getAction (). Equals ( "LOCATION_REACHED")) {Intent intent1 = new Intent (getApplicationContext (), LocationReached.class); StartActivity (intent1); }}}; }  

ServiceClass.java

  public class ServiceClass extension extension {int counter = 0; Fixed final integer UPDATE_INTREVAL = 5000; // Private timer timer = new timer (in milliseconds); @Ivarried Public Eye on On Bind (intro ARG0) {return tap; } @Override public int onStartCommand (Intent intent, flags int, int startId) {Toast.makeText (it started, "service", Toast.LENGTH_LONG) .show (); DoSomeThingRepeatedly (); Return START_STICKY; } @ Override Public Empty OnDesto () {Super. Onsteroy (); If (timer! = Null) {timer.cancel (); } Toast. Make text (this, "service is destroyed", toast. LNNGH_LOG). Show (); } Private void doSomeThingRepeatedly () {timer.scheduleAtFixedRate (New TimerTask () {@Override public void run () {Toast.makeText (getApplicationContext () "," + counter, Toast.LENGTH_SHORT) .show (); counter ++; if (counter == 6) {intent broadcastintent = new intent (); broadcastintent.setAction ( "LOCATION_REACHED"); getApplicationContext () sendBroadcast (broadcastintent) ;.}}}, 0, UPDATE_INTREVAL); }}  

andridmanifest.xml

  & lt; Activity Android: name = "com.vallabh.servicesexample.ServicesExampleActivity" Android: label = "@ string / app_name" & gt; & Lt; Intent-Filter & gt; & Lt; Action Android: name = "android.intent.action.MAIN" /> & Lt; Category android: name = "android.intent.category.LAUNCHER" /> & Lt; / Intent-Filter & gt; & Lt; / Activity & gt; & Lt; Service Android: name = "com.vallabh.servicesexample.ServiceClass" & gt; & Lt; / Service & gt; & Lt; Activity Android: name = "com.vallabh.servicesexample.LocationReached" & gt; & Lt; / Activity & gt;  

"Are you not calling unregistered receivers ()?"

means that when the activity is not running, you forget to unregister the broadcast receiver. Use onStop () method for registration of broadcast receivers:

  protected void (@OverrideStop) (if (null! = Intentreceiver) unregisterReceiver (intentreceiver ); super.onStop ();}  

and broadcasting activities onResume method onCreate instead of entering broadcast receiver Broadcast because it will be re-registered when you come back from next activity.


Comments