Hey I've got a OutgoingCallReceiver, which detect when a outgoing call happends. If a special number has been dialed, it's starting an service.
But now, i've got a problem that i need to stop the same service when the that outgoing call has ended.
Is there a nice way to do it? Thanks!!
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);
}
}
}
There is the code.
Thanks for all the help, and any help is appreciated. Thanks!
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;
}
}
Class 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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.