I am writing a small program that gets activated when sms is received.We need to start/stop service through graphical user interface. I am using a check box and a button to start or stop the service.
The code is as followed.
private smsBroadcast b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onclick(View i)
{
CheckBox c=(CheckBox)findViewById(R.id.checkBox1);
b=new smsBroadcast();
try{
if(c.isChecked())
registerReceiver(b, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
else
unregisterReceiver(b);
}
catch(Exception e)
{
}
finish();
}
Here smsBroadcast is the broadcastReceiver class.
When we click the button, it is showing some exceptions in logcat.
The exceptions are as followed…
03-31 12:01:33.923: E/ActivityThread(806): Activity examples.sms.SmssmsActivity has leaked IntentReceiver examples.sms.smsBroadcast@405457f0 that was originally registered here. Are you missing a call to unregisterReceiver()?
03-31 12:01:33.923: E/ActivityThread(806): android.app.IntentReceiverLeaked: Activity examples.sms.SmssmsActivity has leaked IntentReceiver examples.sms.smsBroadcast@405457f0 that was originally registered here. Are you missing a call to unregisterReceiver()?
03-31 12:01:33.923: E/ActivityThread(806): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:756)
03-31 12:01:33.923: E/ActivityThread(806): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:551)
03-31 12:01:33.923: E/ActivityThread(806): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:795)
03-31 12:01:33.923: E/ActivityThread(806): at android.app.ContextImpl.registerReceiver(ContextImpl.java:782)
03-31 12:01:33.923: E/ActivityThread(806): at android.app.ContextImpl.registerReceiver(ContextImpl.java:776)
03-31 12:01:33.923: E/ActivityThread(806): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
03-31 12:01:33.923: E/ActivityThread(806): at examples.sms.SmssmsActivity.onclick(SmssmsActivity.java:25)
03-31 12:01:33.923: E/ActivityThread(806): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 12:01:33.923: E/ActivityThread(806): at java.lang.reflect.Method.invoke(Method.java:507)
03-31 12:01:33.923: E/ActivityThread(806): at android.view.View$1.onClick(View.java:2139)
03-31 12:01:33.923: E/ActivityThread(806): at android.view.View.performClick(View.java:2485)
03-31 12:01:33.923: E/ActivityThread(806): at android.view.View$PerformClick.run(View.java:9080)
03-31 12:01:33.923: E/ActivityThread(806): at android.os.Handler.handleCallback(Handler.java:587)
03-31 12:01:33.923: E/ActivityThread(806): at android.os.Handler.dispatchMessage(Handler.java:92)
03-31 12:01:33.923: E/ActivityThread(806): at android.os.Looper.loop(Looper.java:123)
03-31 12:01:33.923: E/ActivityThread(806): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-31 12:01:33.923: E/ActivityThread(806): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 12:01:33.923: E/ActivityThread(806): at java.lang.reflect.Method.invoke(Method.java:507)
03-31 12:01:33.923: E/ActivityThread(806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-31 12:01:33.923: E/ActivityThread(806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-31 12:01:33.923: E/ActivityThread(806): at dalvik.system.NativeStart.main(Native Method)
you need to register the receiver in onResume and unregister in onPause
http://developer.android.com/reference/android/content/BroadcastReceiver.html
If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won’t receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won’t be called if the user moves back in the history stack.