嘿,我有一个OutgoingCallReceiver,可以检测拨出电话的时间。如果已拨打特殊号码,则表示正在启动服务。
但是现在,我遇到了一个问题,即当该传出呼叫结束时我需要停止相同的服务。
有一个很好的方法吗?谢谢!
ublic class OutgoingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
boolean ringing = false, offhook = false;
Intent i = new Intent(arg0, MyServices.class);
Intent ii = new Intent(arg0, RecorderService.class);
Bundle bundle = arg1.getExtras();
if(null == bundle)
return;
String phonenumber = arg1.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("OutgoingCallReceiver",phonenumber);
Log.i("OutgoingCallReceiver",bundle.toString());
if(phonenumber.equals("#1234") || bundle.toString().equals("#1234")) {
ii.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startService(ii);
}
String action = arg1.getAction();
if (Intent.ACTION_NEW_OUTGOING_CALL.equals(action)) {
} else{
arg0.stopService(ii);
}
}
}
有代码。
感谢所有帮助,感谢任何帮助。谢谢!
答案 0 :(得分:0)
Class MyService:
public class MyService extends Service {
private CallListener call = null;
public MyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
call = new CallListener(getApplicationContext());
call.start();
return(START_STICKY);
}
@Override
public void onDestroy() {
call.stop();
call.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
类CallListener:
public class CallListener {
private CallStateListener callStateListener = null;
private OutgoingReceiver outgoingReceiver = null;
public CallListener(Context context) {
this.context = context;
callStateListener = new CallStateListener();
outgoingReceiver = new OutgoingReceiver();
}
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
doSomething1();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
doSomething2();
break;
case TelephonyManager.CALL_STATE_RINGING:
doSomething3();
break;
}
}
}
public class OutgoingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
doSomething4();
}
}
public void start() {
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
context.registerReceiver(outgoingReceiver, intentFilter);
}
public void stop() {
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
context.unregisterReceiver(outgoingReceiver);
}
}