2015-04-23 72 views
0

我正在嘗試構建一個應用程序,其中相機閃光燈將在通話和短信傳入活動中發生。但相機閃光燈在通話過程中沒有打開...我寫了簡單的程序。在通話過程中無法打開相機閃光燈

public class MainActivity extends Activity { 
    ..... 
    .... 

    public static class AlertHandler extends BroadcastReceiver { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
      System.out.println("===inside oneceive"); 
      boolean incomingCall = false; 
      Parameters alertParams; 
       Camera alertCamera; 
       String callState = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
       if (null != callState && callState.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
        incomingCall = true; 
       } 
       System.out.println("===incomingCall =="+incomingCall); 

       if(incomingCall){ 
        System.out.println("OPENING CAMERA..."); 
        alertCamera = Camera.open(); 
        alertParams = alertCamera.getParameters(); 
        alertParams.setFlashMode(Parameters.FLASH_MODE_TORCH); 
        alertCamera.setParameters(alertParams); 
        alertCamera.startPreview(); 
       }     

       } 
      } 
     } 

我得到了sysout開放相機好,但現在閃光。 請幫助一些代碼修復。 logcat沒有錯誤。

在這個應用程序玩的存儲區很多,它們是如何工作的?

+0

你需要有一個線程和while循環,它將處理在手電筒 – VVB

+0

閃爍我不想來電期間blinking..just打開閃光燈...... – Abhijit

回答

1

使用此其工作

public class MainActivity extends ActionBarActivity { 

private static Camera camera; 
// flag to detect flash is on or off 
private static boolean isLighOn = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    camera = Camera.open(); 

} 

public static void toggle() { 

    if (isLighOn) { 

     Log.i("info", "torch is turn off!"); 
     final Parameters p = camera.getParameters(); 
     p.setFlashMode(Parameters.FLASH_MODE_OFF); 
     camera.setParameters(p); 
     camera.stopPreview(); 
     isLighOn = false; 

    } else { 

     Log.i("info", "torch is turn on!"); 
     final Parameters p = camera.getParameters(); 
     p.setFlashMode(Parameters.FLASH_MODE_TORCH); 

     camera.setParameters(p); 
     camera.startPreview(); 
     isLighOn = true; 

    } 

} 

} 

public class AlertHandler extends BroadcastReceiver { 

boolean incomingCall = false; 

@Override 
public void onReceive(Context context, Intent intent) { 

    String callState = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 

    if (null != callState 
      && callState.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
     incomingCall = true; 

    } 

    if (incomingCall) { 
     MainActivity.toggle(); 
    } else { 
     MainActivity.toggle(); 
    } 

} 
} 
+0

喜sandeep,我只是分解我的方法作爲你的建議..但它不是打開閃光燈...我已調試,得到正確的系統輸出在打開和關閉方法,但不打開閃光燈在通話期間.. :( – Abhijit

+0

你有添加這些權限<使用權限android:name =「android.permission.CAMERA」/> <使用權限android:name =「android.permission.READ_PHONE_STATE」/> <使用功能android:name =「android.hardware.camera」/> –

+0

是......他們在場......權限在那裏......還有接收者在清單中的意圖...... – Abhijit