我希望在手机响铃时闪烁我的Android手机闪光灯,并且在响铃时间结束,拒绝接听电话或接听电话时应停止闪光灯闪烁。
我使用过广播接收器,但效果不佳。
答案 0 :(得分:0)
public class PhoneCallReceiver extends BroadcastReceiver {
TelephonyManager telephony;
SharedPreferences sp;
PhoneCallStateListener customPhoneListener;
boolean entered = false;
AudioManager audioManager;
Camera camera;
Camera.Parameters params;
@Override
public void onReceive(Context context, Intent intent) {
sp = context.getSharedPreferences("flash_setings", 0);
telephony =
(TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
customPhoneListener = new PhoneCallStateListener(context);
telephony.listen(customPhoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
public class
PhoneCallStateListener extends PhoneStateListener {
private Context
context;
Timer timer;
TimerTask timerTask;
public PhoneCallStateListener(Context context) {
this.context = context;
}
@Override
public void onCallStateChanged(final int state, String
incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d("STATE", "CALL_STATE_RINGING");
Log.d("ringing state",
"ringing");
TelephonyManager telephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class
clazz = Class.forName(telephonyManager.getClass().getName());
Method
method = clazz.getDeclaredMethod("getITelephony");
method.setAccessible(true);
ITelephony telephonyService =
(ITelephony) method.invoke(telephonyManager); // Checking incoming
call number //System.out.println("Call " + block_number); if
(sp.getBoolean("isenabled", true) == true) {
if (entered == false) {
entered = true;
Log.d("SOUND OFF :" +
sp.getBoolean("flash_only_isenabled", false) + "", "");
if
(sp.getBoolean("flash_only_isenabled", false) == true) {
Log.d("SOUND OFF :" + sp.getBoolean("flash_only_isenabled", false) +
"", "");
audioManager = (AudioManager)
context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
} else {
Log.d("SOUND", "ON");
audioManager = (AudioManager)
context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_RING, false);
}
if
(sp.getBoolean("flash_ring_isenabled", false) == true) {
}
if
(camera == null) {
try {
Log.d("Camera ", "interval " +
sp.getInt("flash_interval", 1) * 1000);
String myString =
"010101010101010101010101";
long blinkDelay =
sp.getInt("flash_interval", 1) * 1000;
for (int i = 0; i <
myString.length(); i++) {
if (myString.charAt(i) == '0') {
camera =
Camera.open();
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.startPreview();
camera.stopPreview();
camera.release();
camera = null;
try {
Thread.sleep(blinkDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
camera = Camera.open();
params =
camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
camera.stopPreview();
camera.release();
camera = null;
}
}
} catch (Exception e) {
if
(camera != null) {
camera.stopPreview();
camera.release();
camera =
null;
}
}
}
}
}
} catch (Exception e) { //Toast.makeText(context,
e.toString(), Toast.LENGTH_LONG).show();
} break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d("STATE", "CALL_STATE_IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("STATE", "CALL_STATE_OFFHOOK");
break;
}
super.onCallStateChanged(state, incomingNumber);
telephony.listen(customPhoneListener,
PhoneStateListener.LISTEN_NONE);
}
}
}