打开开关监听来电,来电时控制闪光灯闪烁
1、SystemUI/ext/src/com/mediatek/systemui/ext/QuickSettingsTileViewId.java
增加ID :
在 QuickSettingsTileViewId 方法中加上此开关的ID: ID_INCALL_FLASHING_LED,
2、SystemUI/src/com/android/systemui/statusbar/phone/QuickSettings.java
确认开关的位置
在 addSystemTiles 方法中加上要加入的 代码
private void addSystemTiles(ViewGroup parent, LayoutInflater inflater) {
...
addInCallFlashingLedTile(parent, inflater);
}
addInCallFlashingLedTile的具体实现
private void addInCallFlashingLedTile(ViewGroup parent, LayoutInflater inflater) {
final QuickSettingsBasicTile inCallFlashLed = new QuickSettingsBasicTile(mContext);
boolean enabled = Settings.System.getInt(mContext.getContentResolver(), "incall_flashing_led", 0) != 0; //开关默认关闭
if (enabled) {
inCallFlashLed.setImageResource(R.drawable.ic_incall_flashing_led_enabled);
} else {
inCallFlashLed.setImageResource(R.drawable.ic_incall_flashing_led_disenabled);
}
inCallFlashLed.setTextResource(R.string.quick_settings_incall_flashing_led);
inCallFlashLed.setTileViewId(QuickSettingsTileViewId.ID_INCALL_FLASHING_LED);
inCallFlashLed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean enabled = Settings.System.getInt(mContext.getContentResolver(), "incall_flashing_led", 0) != 0;
if (enabled) {
Settings.System.putInt(mContext.getContentResolver(), "incall_flashing_led", 0);
inCallFlashLed.setImageResource(R.drawable.ic_incall_flashing_led_disenabled);
} else {
Settings.System.putInt(mContext.getContentResolver(), "incall_flashing_led", 1);
inCallFlashLed.setImageResource(R.drawable.ic_incall_flashing_led_enabled);
}
}
});
parent.addView(inCallFlashLed);
}
图片和字符串资源需添加
3、SystemUI/src/com/android/systemui/SystemUIService.java
监听来电及打开摄像头闪光灯的关键代码
添加代码:
private static final long OPEN_DELAYED_TIME = 300; // 闪烁频率
private static final long CLOSE_DELAYED_TIME = 500;//间隔
private TelephonyManager mTelephonyManager;
private Camera mCamera;
private Parameters mParameter;
private Handler mFlashHandler;
private boolean mIsInCall = false;
private boolean mIsOpen = false;
private void initInCallFlashLed() {
mIsInCall = false;
mIsOpen = false;
if (true) {
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(MyPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
mFlashHandler = new Handler();
}
}
private void releaseInCallFlashLed() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mParameter = null;
mCamera = null;
}
mFlashHandler.removeCallbacks(mFlashRunnable);
mTelephonyManager = null;
mFlashHandler = null;
}
@Override
public void onDestroy() {
super.onDestroy();
releaseInCallFlashLed();
}
private PhoneStateListener MyPhoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
mIsInCall = false;
closeFlashLight();
break;
case TelephonyManager.CALL_STATE_RINGING:
mIsInCall = true;
openFlashLight();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
closeFlashLight();
break;
}
}
};
private void openFlashLight() {
if (mIsInCall) {
try {
if(mCamera == null){
mCamera = Camera.open();
}
mCamera.startPreview();
mParameter = mCamera.getParameters();
boolean enabled = Settings.System.getInt(getContentResolver(), "incall_flashing_led", 0) != 0;
if (enabled) {
mFlashHandler.post(mFlashRunnable);
}
} catch (java.lang.RuntimeException e) {
Log.d(TAG, "openFlashLight=>error: " + e.toString());
}
}
}
private void closeFlashLight() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mParameter = null;
mCamera = null;
}
mFlashHandler.removeCallbacks(mFlashRunnable);
}
private Runnable mFlashRunnable = new Runnable() {
@Override
public void run() {
if (mIsOpen) {
mIsOpen = false;
if (mCamera != null && mParameter != null) {
mParameter.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(mParameter);
mFlashHandler.postDelayed(mFlashRunnable, OPEN_DELAYED_TIME);
}
} else {
mIsOpen = true;
if (mCamera != null && mParameter != null) {
mParameter.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(mParameter);
mFlashHandler.postDelayed(mFlashRunnable, CLOSE_DELAYED_TIME);
}
}
}
};
导入包:
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.os.Handler;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.content.Context;
import android.provider.Settings;
最后在onCreate()方法中加上 initInCallFlashLed();
public void onCreate() {
...
initInCallFlashLed();
}
注意在AndroidManifest.xml中加入权限:
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA"/>