1

我对由 PhoneStateListener 启动时的服务行为有疑问。

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

所以我写了一个广播接收器注册一个PhoneStateListener。此侦听器启动一项服务,以在没有预览的情况下从正面摄像头拍照。问题在于时机。该服务在图片可用之前被杀死!但是为什么服务会被 SO 杀死?

通过广播接收器:

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();
    }
}

权限没问题,代码有时工作有时不工作,因为服务被杀死了。

有任何想法吗?

4

1 回答 1

0

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

于 2013-07-27T11:59:16.753 回答