我有一个活动和服务的应用程序。
public class ClsService extends Service {
private NotificationManager notificationManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
this.notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
// other stuffs working great!
}
@Override
public void onDestroy() {
this.notificationManager.cancelAll();
super.onDestroy();
}
}
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
CheckBox serviceEnabler = (CheckBox)findViewById(R.id.serviceEnabler);
serviceEnabler.setChecked(true);
serviceEnabler.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
startService(new Intent(this, MainService.class));
} else {
stopService(new Intent(this, MainService.class));
}
}
});
}
}
我的问题是:
我创建服务的方式按预期工作,如果我关闭服务继续运行的活动,但每次我尝试停止然后在NullPointerException
this.notificationManager.cancelAll();
的活动
java.lang.RuntimeException: Unable to stop service com.idt.march.ClsService@45fabf30: java.lang.NullPointerException
at android.app.ActivityThread.handleStopService(ActivityThread.java:3090)
at android.app.ActivityThread.access$3700(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2099)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.idt.march.ClsService.onDestroy(MainService.java:155)
at android.app.ActivityThread.handleStopService(ActivityThread.java:3076)
at android.app.ActivityThread.access$3700(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2099)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您无法在另一个线程上运行该服务,但您可以在服务中创建新线程。创建线程的方式与在Activity中完全相同。
关于你的NullPointerException尝试添加一个检查
if (this.notificationManager == null)
this.notificationManager.cancelAll();
虽然您可能会重新考虑使用cancelAll()
,因为根据文档,它会清除所有通知。
答案 1 :(得分:0)
如果要在后台线程中运行服务,请考虑使用IntentService。它与服务并不完全相同,但如果它适合您希望服务执行的工作,则它非常有用。