PhoneStateListener启动的服务在完成之前被终止

时间:2013-07-16 16:48:01

标签: android service

我有一个关于PhoneStateListener启动时的服务行为的问题。

我的客户想要一个安全软件来跟踪被盗设备。我正在写的功能在按下SEND按钮时拍照。

所以我写了一个注册PhoneStateListener的broadcastreceiver。此监听器启动服务,无需预览即可从摄像头拍摄照片。问题是时机。在图片可用之前,该服务正在被杀死!但为什么服务被SO杀死?

通过Broadcastreceiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class PhoneReceiver  extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    ATMPhoneStateListener phoneListener=new ATMPhoneStateListener(context);
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}
} 

PhoneStateListener:

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.content.Context;
import android.content.Intent;
import java.lang.reflect.Method;
import android.os.IBinder;
import android.os.Binder;
import com.my.services.TakePictureService;

public class MyPhoneStateListener extends PhoneStateListener {

private Context context;

public MyPhoneStateListener (Context context) {
    this.context = context;
}

public void onCallStateChanged(int state, String incomingNumber){
    switch(state)
    {
       case TelephonyManager.CALL_STATE_IDLE:
            break;
       case TelephonyManager.CALL_STATE_OFFHOOK:
           phoneIsOffHeek(); 

            break;
       case TelephonyManager.CALL_STATE_RINGING:
           break;
    }
}   

@SuppressWarnings({ "rawtypes", "unchecked" })
private void phoneIsOffHeek() {
        Intent takePictureService = new Intent(context, TakePictureService.class);
        context.startService(takePictureService);           
}
}

服务:

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;

public class TakePictureService extends Service {

private Context context;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    context = this.getApplicationContext();
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    if (GBCameraUtil.findFrontFacingCamera() != -1) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmDDhhmmss");
        String date = dateFormat.format(new Date());
        final String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + date + ".jpg";  

        GBTakePictureNoPreview c = new GBTakePictureNoPreview(context, this);
        c.setUseFrontCamera(true);
        c.setFileName(fileName);

        if (c.cameraIsOk()) {
            try {
                c.takePicture();
            } catch (Exception e) {
            }
        }

    } else {
    stopSelf();
    }
}

权限没问题,代码有时会运行,有时则不运行,因为服务已被终止。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我发现了我的问题。该服务启动了两次,第二次启动就是杀死了第一个。