【问题标题】:How to send sms?如何发送短信?
【发布时间】:2019-08-08 06:47:59
【问题描述】:

我想发送短信,但不允许。

错误是

java.lang.SecurityException:发送短信:uid 10195 没有 android.permission.SEND_SMS。

请帮我解决它。

protected void sendMessage(String number , String msh)
    {
        try {

            PendingIntent sentPi    = PendingIntent.getBroadcast(this,0,new Intent("sent"),0);
            PendingIntent deliver   = PendingIntent.getBroadcast(this,0,new Intent("delivered"),0);

            SmsManager smsManager   = SmsManager.getDefault();
            smsManager.sendTextMessage(number,null,msh,sentPi,deliver);

        }
        catch (Exception e){
             Log.d("SMS_k",e.toString());
        }
    }

【问题讨论】:

标签: android sms


【解决方案1】:

首先你需要把它放在你的manifest.xml

<uses-permission android:name="android.permission.SEND_SMS" />

然后你可以这样做发送短信

try{
    SmsManager smgr = SmsManager.getDefault();
    smgr.sendTextMessage("ANY NUMBER",null,"YOUR MESSAGE GOES HERE",null,null);
    Toast.makeText(MainActivity.this, "SMS Sent Successfully", Toast.LENGTH_SHORT).show();
}
catch (Exception e){
    Toast.makeText(MainActivity.this, "SMS Failed to Send", Toast.LENGTH_SHORT).show();
}

如果您想通过 Intent 进行操作,您只需要 添加这个:

Intent intent=new Intent(getApplicationContext(),YOURACTIVITY.class);  
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);  

try{
    SmsManager sms=SmsManager.getDefault();  
    sms.sendTextMessage("ANY NUMBER", null, "YOUR MESSAGE GOES HERE", pi,null);
}catch(Exception e){
    //Something wrong happened
}  

【讨论】:

  • try catch 仍然是必要的,因为没有 sim 卡的平板电脑和设备在尝试执行此操作时仍会抛出异常。只是一个小小的 FYI 来自被平板电脑撞毁的人:D
猜你喜欢
  • 2011-02-17
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
相关资源
最近更新 更多