从服务调用线程

时间:2013-11-27 12:14:32

标签: android multithreading service

我可以从服务启动线程吗?

我想录制正在进行的音频信号并检测我的应用中的哨声,这应该在应用关闭后运行。对于这个应用程序,我有两个线程,一个是连续录制音频,另一个是检测哨子(我使用了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 com.musicg.demo.android.DetectAndLockService@4105d438 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

`

我哪里错了?

2 个答案:

答案 0 :(得分:0)

您必须AsyncTask执行此操作。 http://developer.android.com/reference/android/os/AsyncTask.html

您必须重新实施doInBackground方法。

答案 1 :(得分:0)

如果VariableClass.get_detection_running()方法返回false,那么在调用方法之前,您不会检查null或初始化recorderThreaddetectorThread个实例。所以你得到NullPointerException这样做:

    if (VariableClass.get_detection_running()) {
        ///...your code here..
    } else {
        if(null !=recorderThread)
        recorderThread.stopRecording();
        if(null !=detectorThread
        detectorThread.stopDetection();
    }