我创建了一个BroadcastReceiver,可以监听他的电话状态并采取相应的措施。
这里的问题是,它似乎没有从我的数据库中获取正确的值,从一开始,处于空闲状态的吐司显示了两次,而在第二次调用之后显示了四次,依此类推...
我想我的课上有毛病,但我不知道该怎么办...
这是一个代码片段:
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;
}
}
}
}
最佳答案
您可能在onCallStateChanged
内部做了大量工作(数据库I / O操作)。我在这里的BroadcastReceiver
也有类似的问题:Changing ringer volume during ringing。为了进行测试,请尝试对BroadcastReceiver
中的静态字段(而不是LiteSQL)进行操作,并检查是否有帮助。
关于android - PhoneStateListener和BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14503414/