【问题标题】:sending message in android two times instead of once在android中发送消息两次而不是一次
【发布时间】:2015-08-09 09:54:28
【问题描述】:

我想在通话结束后发送短信,我的应用程序发送短信,但问题是每次通话都会发送两次,我不知道问题出在哪里(我想每次都发送一条短信不是两次)

这是我的代码:

public class Receiver extends BroadcastReceiver {  
  SmsManager smsmanager=SmsManager.getDefault(); 
  public void onReceive(Context context, Intent intent) {

  if (phoneNumber != null){

     telephonyService.endCall();
     smsmanager.sendTextMessage(phoneNumber, null, "message", null, null);

  }
}

在清单上:

<receiver  android:name=".Receiver">
        <intent-filter  android:priority="100" >
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

【问题讨论】:

  • 您的Receiver 注册人怎么样?与whichIntentFilter?
  • @SimonMarquis 我已添加到问题中

标签: android broadcastreceiver sendmessage smsmanager


【解决方案1】:

您的Receiver 将在每次电话状态发生变化(正在响铃、通话中……)时被呼叫。
您必须过滤相应的操作:

  // Keep track of the previous state
  String previousState;

  public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if (CALL_STATE_IDLE.equals(state) && CALL_STATE_OFFHOOK.equals(previousState) {
      // send sms here
    }
    previousState = state;
  }

【讨论】:

  • 坦克你太棒了,稍微改变你的代码就可以了,thanx :) @SimonMarquis
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 2020-05-13
  • 2020-12-14
  • 2020-06-13
  • 1970-01-01
相关资源
最近更新 更多