You may be doing to much inside the onCallStateChanged
(database I/O operations). I've had similar issue with the BroadcastReceiver
here: Changing ringer volume during ringing . For testing try to operate on static fields (instead of LiteSQL) inside BroadcastReceiver
and check if it helps.
PhoneStateListener and BroadcastReceiver
-
17-01-2022 - |
Question
I've created a BroadcastReceiver that listens to he phone states and act accordingly.
The problem here is that it doesn't seem to take the right values from my database, and from the beginning the toast in idle state shows twice, and after a second call shows four times etc...
I guess there is something wrong with my class but I can't figure out what...
Here is a code snippet :
public class PhoneListener extends BroadcastReceiver{
private DBAdapter db;
private Cursor data;
private Cursor options;
private int activer;
private int mouvement;
private int verticalite;
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}
public void stopService(Context context){
Intent alerte = new Intent(context, SensorOrientation.class);
alerte.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.stopService(alerte);
}
public void startService(Context context){
Intent alerte = new Intent(context, SensorOrientation.class);
alerte.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(alerte);
}
public void startnotif(Context context){
Intent intent = new Intent(context, SecuriteHauteReboot.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(intent);
}
public class CustomPhoneStateListener extends PhoneStateListener {
//private static final String TAG = "PhoneStateChanged";
Context context; //Context to make Toast if required
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
db = new DBAdapter(context);
data = db.selectWidget();
options = db.selectVerticalite();
activer = data.getInt(data.getColumnIndex("activer"));
mouvement = options.getInt(options.getColumnIndex("activer_alarme_mouvement"));
verticalite = options.getInt(options.getColumnIndex("activer_alarme_verticalite"));
data.close();
options.close();
//when Idle i.e no call
Toast.makeText(context,
String.format(" "+mouvement+" "+activer),
Toast.LENGTH_SHORT).show();
if(activer == 1){
if(mouvement == 1 || verticalite == 1){
Toast.makeText(context,
String.format("and I'm here...."),
Toast.LENGTH_SHORT).show();
startService(context);
startnotif(context);
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//when Off hook i.e in call
//Make intent and start your service here
if (activer == 1){
if(mouvement == 1 || verticalite == 1){
stopService(context);
}
}
break;
case TelephonyManager.CALL_STATE_RINGING:
//when Ringing
if (activer == 1){
if(mouvement == 1 || verticalite == 1){
Toast.makeText(context,
String.format("and I'm here...."),
Toast.LENGTH_SHORT).show();
stopService(context);
}
}
break;
default:
break;
}
}
}
}
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow