【问题标题】:Send sms after call rejection - being sent multiple times呼叫拒绝后发送短信 - 被多次发送
【发布时间】:2015-08-25 06:35:34
【问题描述】:

我想在拒绝来电后发送短信。 应用程序正在发送短信,但问题是它发送了两次。我无法弄清楚问题出在哪里。

下面是使用的代码。

public class CallBarring extends BroadcastReceiver
{
    private String number;

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        // If, the received action is not a type of "Phone_State", ignore it
         if (!intent.getAction().equals("android.intent.action.PHONE_STATE")) 
             return;

        // Else, try to do some action
         else 
         {
             // Fetch the number of incoming call
             number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

             // Check, whether this is a member of "Black listed" phone numbers stored in the database
            if(MainActivity.blockList.contains(new Blacklist(number)))
             {
                 // If yes, invoke the method
                 disconnectPhoneItelephony(context);
                 sendSMS(context);
               //  return;
             }else{
                 disconnectPhoneItelephony(context);
                 sendSMS(context);

             }
         }  
    }

    public void sendSMS(Context context) {

        try{
        String message = SharedPrefActivity.CommonMethod.getPrefsData(context, SharedPrefActivity.Constants.TextMessage, "");

        Intent intent=new Intent(context,CallBarring.class);  
        PendingIntent pi=PendingIntent.getActivity(context, 0, intent,0);  
        SmsManager sms=SmsManager.getDefault();  
        sms.sendTextMessage(number, null, message, pi,null);
        Toast.makeText(context, "SMS Sent", Toast.LENGTH_LONG).show();

        } catch (Exception e)
        {
            Toast.makeText(context, "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }      
    }

    // Method to disconnect phone automatically and programmatically 
    // Keep this method as it is 
     @SuppressWarnings({ "rawtypes", "unchecked" })
     private void disconnectPhoneItelephony(Context context) 
     {
         ITelephony telephonyService;
         TelephonyManager telephony = (TelephonyManager) 
         context.getSystemService(Context.TELEPHONY_SERVICE);  
         try 
         {
             Class c = Class.forName(telephony.getClass().getName());
             Method m = c.getDeclaredMethod("getITelephony");
             m.setAccessible(true);
             telephonyService = (ITelephony) m.invoke(telephony);
             telephonyService.endCall();
         } 
         catch (Exception e) 
         {
             e.printStackTrace();
         }
     }
}

【问题讨论】:

  • 对不起,你的 if 和 else 块中的代码不一样吗?那有什么意义呢?
  • MainActivity.blockList 是静态的吗?如果是,你是怎么填的?您正在调用相同的断开连接条件并发送短信,那为什么会这样?
  • 代码不起作用....
  • 让删除其他部分然后无法工作
  • 删除其他部分..但同样的条件问题请帮助先生....

标签: android telephonymanager smsmanager


【解决方案1】:

试试这个:

// Check, whether this is a.   member of "Black listed" phone.   numbers stored in the database
            if(MainActivity.blockList.contains(new Blacklist(number)))
     {
         // If yes, invoke the method
                 disconnectPhoneItelephony(context);
         sendSMS(context);
       //  return;
     }else{
                 disconnectPhoneItelephony(context);
         sendSMS(context);

     }

这个给

// Check, whether this is a.       member of "Black listed" phone.       numbers stored in the database
                if(MainActivity.blockList.contains(new Blacklist(number)))
     {
         // If yes, invoke the method
                 disconnectPhoneItelephony(context);
sendSMS(context)
         } 

【讨论】:

  • 删除其他部分。同样的条件问题请帮助我....我从两天开始尝试
猜你喜欢
  • 2012-03-23
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 2016-04-29
相关资源
最近更新 更多