-1

i am working on service, what actually i want is to call some event every minute, suppose i want to print log at every minute, what code i did its working fine but only when the activity is on foreground, when i am minimizing the app or closing it, it gives error as below :

08-19 17:31:33.936: E/AndroidRuntime(14264): FATAL EXCEPTION: main
08-19 17:31:33.936: E/AndroidRuntime(14264): java.lang.RuntimeException: Unable to stop activity    {com.example/com.example.MainActivity}: java.lang.IllegalArgumentException: Receiver not registered:   com.example.MainActivity$1@41117730
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3629)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3675)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.app.ActivityThread.access$900(ActivityThread.java:162)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.os.Looper.loop(Looper.java:158)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.app.ActivityThread.main(ActivityThread.java:5751)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at java.lang.reflect.Method.invokeNative(Native Method)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at java.lang.reflect.Method.invoke(Method.java:511)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at dalvik.system.NativeStart.main(Native Method)
08-19 17:31:33.936: E/AndroidRuntime(14264): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.example.MainActivity$1@41117730
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:719)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1620)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:445)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at com.example.MainActivity.onStop(MainActivity.java:143)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1305)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.app.Activity.performStop(Activity.java:5338)
08-19 17:31:33.936: E/AndroidRuntime(14264):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3626)
08-19 17:31:33.936: E/AndroidRuntime(14264):    ... 11 more

My code in activity is as below:

public class MainActivity extends Activity 
{


BroadcastReceiver mBroadcastTime;

@Override
public void onCreate(Bundle savedInstanceState) 
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


mBroadcastTime = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service1 = new Intent(context, MyAlarmService.class);
        context.startService(service1);
    }

};

IntentFilter mTime = new IntentFilter(Intent.ACTION_TIME_TICK);
registerReceiver(mBroadcastTime, mTime);


}

@Override
public void onPause()
{
  super.onPause();
  if(mBroadcastTime != null){
      unregisterReceiver(mBroadcastTime);
      Log.v("BROADCAST ON PAUSE", "UNREGISTERED");
  }
}

@Override
public void onStop()
{
  super.onStop();
  if(mBroadcastTime != null){
      unregisterReceiver(mBroadcastTime);
      Log.v("BROADCAST ON STOP", "UNREGISTERED");
  }
}

@Override
public void onDestroy()
{
  super.onDestroy();
  if(mBroadcastTime != null){
      unregisterReceiver(mBroadcastTime);
      Log.v("BROADCAST ON DESTROY", "UNREGISTERED");
  }
}

}

Rajesh Panchal
  • 1,140
  • 3
  • 20
  • 39

2 Answers2

3

The problem is that you are trying to unregister more than once broadcast receiver. You should check two things: Is object null or not Is registered or not

Please follow to this question or siply remove unregister method from onStop() and onDestroy()

Remeber also that if you unregister broadcast at onPause you should register it once again in onResume()

So simplest way IMO is to:

  • remove unregister from onStrop() and onDestroy()
  • move register broadcast from onCreate() to onResume()

Please take a look into activity lifecycle in Android - that should know every Android Developer ;) - here everything you need

Community
  • 1
  • 1
Robert
  • 1,272
  • 4
  • 21
  • 40
  • ok i did it, and its not giving any error now, but what i want is if i close my app the service should run , it should fire some event(whatever i want) every minute. for that what should i do? – Rajesh Panchal Aug 19 '14 at 12:32
  • You have two versions of services started and binded if you call startService - as you do is should run in background but you have no registered broadcast receiver. Moreover there is no 100% warranty that the service will not be killed by android. To increase the chance that service will not be killed create service with notification. – Robert Aug 19 '14 at 12:40
  • ok, please vote up the question if you think it can be useful to others. Thank you for your help. – Rajesh Panchal Aug 22 '14 at 07:19
0

You register your receiver in onCreate and u unregister it in onPause. The problem is that in onCreate is registered only once. So if the onPause is called at least once then you wont be able to unregister it twice. So best thing to do is register your receiver in onResume