服务在线程中运行

时间:2014-12-20 12:13:17

标签: java android multithreading service

我有一个服务类正在拍照并使用相机将其写入SD卡。现在我希望我每10秒钟调用一次该服务,这样我就可以每隔10秒从摄像机拍摄一次图像。但是我得到例外。这是我的主要活动代码:

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(com.example.picturesenderbluetooth.R.layout.capture_and_send);

    Button cap = (Button)findViewById(R.id.button1);
    cap.setOnClickListener(new OnClickListener() {

        @Override 
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new Thread(new Task()).start();
        }
    });

    }

public void turnbluetoothon()
{
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
    if (mBluetoothAdapter.disable()) {
        mBluetoothAdapter.enable(); 
    } 
}


class Task implements Runnable { 

    @Override 
    public void run() { 
        while(true) { 
            try { 
                Log.w("Rami code yaar","Rami mei phir chal gaya... hahaha");
                startService(new Intent(getBaseContext(), Backgroundservice.class));
                Thread.sleep(10000); 
                stopService(new Intent(getBaseContext(), Backgroundservice.class));

            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
        } 
  } 

 }
}

logcat显示:

12-20 17:11:25.854: E/AndroidRuntime(9859): FATAL EXCEPTION: main
12-20 17:11:25.854: E/AndroidRuntime(9859): java.lang.RuntimeException: Unable to start service com.example.Picture_Sender_Bluetooth.Backgroundservice@419b5920 with Intent { cmp=com.example.picturesenderbluetooth/com.example.Picture_Sender_Bluetooth.Backgroundservice }: java.lang.RuntimeException: Fail to connect to camera service
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2829)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.app.ActivityThread.access$1900(ActivityThread.java:156)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.os.Looper.loop(Looper.java:153)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.app.ActivityThread.main(ActivityThread.java:5336)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at java.lang.reflect.Method.invokeNative(Native Method)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at java.lang.reflect.Method.invoke(Method.java:511)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at dalvik.system.NativeStart.main(Native Method)
12-20 17:11:25.854: E/AndroidRuntime(9859): Caused by: java.lang.RuntimeException: Fail to connect to camera service
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.hardware.Camera.native_setup(Native Method)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.hardware.Camera.<init>(Camera.java:445)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at android.hardware.Camera.open(Camera.java:421)
12-20 17:11:25.854: E/AndroidRuntime(9859):     at com.example.Picture_Sender_Bluetooth.Backgroundservice.onStart(Backgroundservice.java:37)

什么使应用程序崩溃?我该如何解决?

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。在我所做的服务中。我没有放开相机。所以我调用了这个函数。

cameraobject. release();

代码运行正常!