嗨我在来电时闪烁手电筒,我可以使用此代码执行此操作,但问题是当有人参加或切断电话时手电筒没有停止 我试图从各方面停止服务,但手电筒不会停止直到循环完成 在接收器类......
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();
}
}
我正在制作我可以通过接近传感器参加通话的应用程序。此代码有2个意图首先是我工作的版本低于Android 4.0,另一个是headSetUnPluggedintent,即在Android 4.1上工作。所有版本都可以通过哪种方式接听电话
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());
}
}
答案 0 :(得分:0)
这里有几点:
state
的值是没用的,因为你已经在if条件中检查了它。 state是String
(ieimmutable),它的值不会改变。您需要使用TelephonyManager#getCallState()为每次迭代获取呼叫的当前状态。这样的事情(未经测试):
TelephonyManager mgr = getSystemService(Context.TELEPHONY_SERVICE);
int callState = mgr.getCallState();
if (callState != TelephonyManager.CALL_STATE_RINGING )
break;
您通常不应该尝试停止IntentService
,它会在所有工作完成后停止(即onHandleIntent
返回时)。
此外,IntentService javadoc表示您不应该调用stopSelf()。