Я пишу небольшую программу, которая активируется при получении смс. Нам нужно запустить / остановить сервис через графический интерфейс пользователя.Я использую флажок и кнопку, чтобы запустить или остановить службу.
Код выглядит следующим образом.
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();
}
Здесь smsBroadcast является классом broadcastReceiver.
Когда мы нажимаем кнопку, в logcat отображаются некоторые исключения.Исключения следующие:
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)