我可以從服務啓動線程嗎?從服務調用線程
我想記錄正在進行的音頻信號並在我的應用程序中檢測到哨聲,即使在應用程序關閉後也應該運行。對於這個應用程序,我有兩個線程,一個是連續錄製音頻,另一個是檢測哨子(我使用了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