2017-03-07 48 views
0

我遇到問題,但沒有看到任何解決方案。我的SOS應用程序僅閃爍一次

我已經創建了一個「SOS」應用程序,它試圖閃爍用戶手機中的手電筒。

我創造一些邏輯,這工作就像

  • 當我點擊2秒後,按鈕閃爍只是startPreview(參見:int mInterval = 2000;

  • 創建布爾標誌,isFlashOn,但這不工作我不知道爲什麼。

請花3分鐘看代碼,也許你也會看到什麼是壞在這裏。

public class SOSFragment extends Fragment { 

    private Context context; 
    private int mInterval = 2000; 
    private Handler mHandler = new Handler(); 
    private Runnable mStatusChecker; 
    private boolean isFlashOn = false; 
    public SOSFragment() { 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     this.context = context; 
    } 

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

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     stopRepeatingTask(); 
    } 

    void stopRepeatingTask() { 
     mHandler.removeCallbacks(mStatusChecker); 
    } 

    private void startRepeatingTask() { 
     mStatusChecker.run(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_sos, container, false); 
     Button btn_exit = (Button) view.findViewById(R.id.btn_exit); 
     Button btn_sos = (Button) view.findViewById(R.id.btn_sos); 
     ((MainActivity) context).checkCameraPermission(); 
     btn_exit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ((MainActivity) context).closeSOSFragment(); 
       ((MainActivity) context).showToolbarFragment(); 
      } 
     }); 
     btn_sos.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startRunnable(); 
      } 
     }); 
     return view; 
    } 

    private void startRunnable(){ 
     mStatusChecker = new Runnable() { 
      @Override 
      public void run() { 
       final Camera cam = Camera.open(); 
       Camera.Parameters p = cam.getParameters(); 
       p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); 
       cam.setParameters(p); 
       if(!isFlashOn){ 
        cam.stopPreview(); 
        isFlashOn = true; 
       }else { 
        cam.startPreview(); 
        isFlashOn = false; 
       } 

      } 
     }; 
     mHandler.postDelayed(mStatusChecker, mInterval); 
     startRepeatingTask(); 
    } 
} 
+0

看看這個問題。我有一段時間沒有編寫任何Android代碼,但它可能有助於http://stackoverflow.com/questions/6242268/repeat-a-task-with-a-time-delay –

回答

1

變化:

Runnable startRunnable = new Runnable() { 
      public void run() { 
       final Camera cam = Camera.open(); 
       Camera.Parameters p = cam.getParameters(); 
       p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); 
       cam.setParameters(p); 
       if(!isFlashOn){ 
        cam.stopPreview(); 
        isFlashOn = true; 
       }else { 
        cam.startPreview(); 
        isFlashOn = false; 
       } 

      } 
     }; 
     mHandler.postDelayed(this, mInterval); 
    } 

開始閃爍

mHandler.post(startRunnable); 

要停止閃爍

mHandler.removeCallbacks(startRunnable); 
相關問題