我可以从服务启动线程吗?从服务调用线程
我想记录正在进行的音频信号并在我的应用程序中检测到哨声,即使在应用程序关闭后也应该运行。对于这个应用程序,我有两个线程,一个是连续录制音频,另一个是检测哨子(我使用了musicg库中的代码)。现在我正在使用服务,以便整个功能始终在后台运行,服务正在调用这两个线程,如下面的代码所示。 问题:一旦启动了应用程序并启动了服务,如果我关闭了应用程序,则会引发错误。
代码:
`package com.musicg.demo.android;
import android.app.ActivityManager;
import android.app.Service;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class DetectAndLockService extends Service implements
OnSignalsDetectedListener {
private DetectorThread detectorThread;
private RecorderThread recorderThread;
private DevicePolicyManager deviceManger;
private ActivityManager activityManager;
private ComponentName compName;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
deviceManger = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
compName = new ComponentName(this, MyAdmin.class);
if (VariableClass.get_detection_running()) {
recorderThread = new RecorderThread();
recorderThread.start();
detectorThread = new DetectorThread(recorderThread);
detectorThread.setOnSignalsDetectedListener(DetectAndLockService.this);
detectorThread.start();
} else {
recorderThread.stopRecording();
detectorThread.stopDetection();
}
return (START_STICKY);
}
@Override
public void onWhistleDetected() {
/*Action to be performed*/
}
@Override
public void onDestroy() {
super.onDestroy();
recorderThread.stopRecording();
detectorThread.stopDetection();
System.out.println("Service is destroyed");
}
}
`
的logcat:
`
11-27 17:01:14.843: W/dalvikvm(6206): threadid=1: thread exiting with uncaught exception (group=0x409f41f8)
11-27 17:01:14.853: E/AndroidRuntime(6206): FATAL EXCEPTION: main
11-27 17:01:14.853: E/AndroidRuntime(6206): java.lang.RuntimeException: Unable to start service [email protected] with null: java.lang.NullPointerException
11-27 17:01:14.853: E/AndroidRuntime(6206): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2376)
11-27 17:01:14.853: E/AndroidRuntime(6206): at android.app.ActivityThread.access$1900(ActivityThread.java:123)
11-27 17:01:14.853: E/AndroidRuntime(6206): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
11-27 17:01:14.853: E/AndroidRuntime(6206): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 17:01:14.853: E/AndroidRuntime(6206): at android.os.Looper.loop(Looper.java:137)
11-27 17:01:14.853: E/AndroidRuntime(6206): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-27 17:01:14.853: E/AndroidRuntime(6206): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 17:01:14.853: E/AndroidRuntime(6206): at java.lang.reflect.Method.invoke(Method.java:511)
11-27 17:01:14.853: E/AndroidRuntime(6206): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-27 17:01:14.853: E/AndroidRuntime(6206): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-27 17:01:14.853: E/AndroidRuntime(6206): at dalvik.system.NativeStart.main(Native Method)
11-27 17:01:14.853: E/AndroidRuntime(6206): Caused by: java.lang.NullPointerException
11-27 17:01:14.853: E/AndroidRuntime(6206): at com.musicg.demo.android.DetectAndLockService.onStartCommand(DetectAndLockService.java:52)
11-27 17:01:14.853: E/AndroidRuntime(6206): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359)
11-27 17:01:14.853: E/AndroidRuntime(6206): ... 10 more
`
我要去哪里错了?
你有没有考虑过使用IntentService? – user1940676