在android中发送消息两次而不是一次

时间:2015-08-09 09:54:29

标签: android broadcastreceiver sendmessage smsmanager

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

这是我的代码:

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);

  }
}

on Manifest:

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

1 个答案:

答案 0 :(得分:0)

每次电话状态发生变化时,都会调用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;
  }