簡體   English   中英

沒有發送短信,即使 toast 顯示它已發送

[英]text sms isn't being sent, even though toast shows that it's sent

我正在嘗試向我的ArrayList中的所有聯系電話發送短信。 我的代碼如下:

try{

    SmsManager sms = SmsManager.getDefault();

    if (customarray.size() != 0) {

          Log.d("contact",String.valueOf(customarray)); //shows all the contact numbers 

          for (int i = 0; i < customarray.size(); i++) {

             Log.d("contact",String.valueOf(customarray.get(i))); //shows each of the contact numbers

             sms.sendTextMessage(customarray.get(i), null, string, null, null);

             Toast.makeText(contacts.this,"Your sms has been sent to "+customarray.get(i),Toast.LENGTH_SHORT).show();

          }
    }

   } catch (Exception e) {

          e.printStackTrace();

   }

我還在清單文件中添加了權限:

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

在這里, customarray包含所有聯系電話。 即使吐司出現, “您的短信已發送到...” ,但短信永遠不會被發送。 為什么?

正如@Joachim Sauer 所建議的,使用PendingIntent有效:

try{

    SmsManager sms = SmsManager.getDefault();

    Intent intent=new Intent(getApplicationContext(),contacts.class);

    PendingIntent pi= PendingIntent.getActivity(getApplicationContext(), 0, intent,0);

    if (customarray.size() != 0) {

          Log.d("contact",String.valueOf(customarray)); //shows all the contact numbers 

          for (int i = 0; i < customarray.size(); i++) {

             Log.d("contact",String.valueOf(customarray.get(i))); //shows each of the contact numbers

             sms.sendTextMessage(customarray.get(i), null, string, pi, null);

             Toast.makeText(contacts.this,"Your sms has been sent to "+customarray.get(i),Toast.LENGTH_SHORT).show();

          }
    }

   } catch (Exception e) {

          e.printStackTrace();

   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM