Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么服务没有停止?_Android_Intentservice - Fatal编程技术网

Android 为什么服务没有停止?

Android 为什么服务没有停止?,android,intentservice,Android,Intentservice,您好,我在接听来电时闪烁手电筒,我可以使用此代码完成此操作,但问题是,当有人接听或切断来电时,手电筒没有停止 我试图从各个方面停止服务,但手电筒不会停止,直到循环完成 在接收器类中 Intent in = new Intent(context, Run.class); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { in.putExtra("state", state);

您好,我在接听来电时闪烁手电筒,我可以使用此代码完成此操作,但问题是,当有人接听或切断来电时,手电筒没有停止 我试图从各个方面停止服务,但手电筒不会停止,直到循环完成 在接收器类中

Intent in = new Intent(context, Run.class);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                in.putExtra("state", state);
                context.startService(in);
            } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                // Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show();
                context.stopService(in);

            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                // Toast.makeText(context, "Offhook", Toast.LENGTH_LONG).show();

                context.stopService(in);
            }
在服务类

Camera cam;
    Parameters p;
    String state;
    String tag="Runserve";
@Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        try {


            cam = Camera.open();
            p = cam.getParameters();

            String myString = "0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101011";
            long blinkDelay = 50;


            for (int i = 0; i < myString.length(); i++) {
state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);

                if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){

                    break;                  

                    }else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                    break;  
                    }               

                if (myString.charAt(i) == '0') {
                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    cam.setParameters(p);
                } else {
                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    cam.setParameters(p);
                }

                    Thread.sleep(blinkDelay);

            }
        }catch (Exception e) {
            // TODO: handle exception
            Log.d(tag, "in catch1");
            Log.d(tag, e.toString());

    }
}else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
    try {
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.release();
    } catch (Exception e) {
        // TODO: handle exception
        Log.d(tag, e.toString());
    }

    stopSelf();
    }else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
        try {

            p.setFlashMode(Parameters.FLASH_MODE_OFF);
            cam.release();
        } catch (Exception e) {
            // TODO: handle exception
            Log.d(tag, e.toString());
        }

        stopSelf();
    }
}
这里有几点:

  • 在for循环中,检查
    state
    的值是无用的,因为您已经在if条件中检查了它。状态是一个
    字符串(即不可变),它的值不会改变
  • 对于每个迭代,您需要使用获取调用的当前状态。类似于此(未经测试):

  • 通常不应尝试停止
    IntentService
    ,当所有工作完成时(即
    onHandleIntent
    返回时),它会自动停止。 此外,还声明不应调用stopSelf()


在切断呼叫或接听呼叫后更新状态值。你完全正确,我更新了这个东西,但它仍然不起作用……好的,我会更新那个东西,我已经更新了我的代码,每次在循环中更新状态,但手电筒仍然没有停止。请发布更新后的代码,否则无法知道发生了什么。您可能会看到我正在获取loopNo的当前调用状态,您仍然从Intent中获取额外的值,该值是Intent启动时的状态,如果你不介意的话,我有一个很小的问题,请你回答一下,也就是在我的代码中,camera.open有时不起作用,它会出现运行时异常,即无法连接camera服务
public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        TelephonyManager mgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        int callState = mgr.getCallState();


        if(callState==TelephonyManager.CALL_STATE_RINGING) {

            callState = mgr.getCallState();
            String x="0.0";
            String y=(String.valueOf(event.values[0]));
                if( x.equals(y)){
                //Toast.makeText(getApplicationContext(), "Proxy", Toast.LENGTH_SHORT).show();
                try{

                    Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);

                    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK));
                    sendOrderedBroadcast(i, "android.permission.CALL_PRIVILEGED");

                    Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
                    headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
                    headSetUnPluggedintent.putExtra("state", 0);
                    headSetUnPluggedintent.putExtra("name", "Headset");
                    sendOrderedBroadcast(headSetUnPluggedintent, null);
                    if(callState==TelephonyManager.CALL_STATE_OFFHOOK){
                        headSetUnPluggedintent=null;
                        i=null;
                    }else if(callState==TelephonyManager.CALL_STATE_IDLE){
                        headSetUnPluggedintent=null;
                        i=null;
                    }

                }catch(Exception e){
                    Log.d(tag, e.toString());
                }
                }               
TelephonyManager mgr = getSystemService(Context.TELEPHONY_SERVICE);
int callState = mgr.getCallState();
if (callState != TelephonyManager.CALL_STATE_RINGING )
    break;