2011-06-15 42 views
9

我來自iphone開發,你不能在後臺發送短信而不要求用戶確認發送。短信可以在Android的背景發送,以便不需要用戶干預?如何在使用Android的BACKGROUND中發送短信?

+4

退房這個 http://stackoverflow.com/questions/5671564/how-to-send-sms-message-on-android-device-in-the-background – sealz 2011-06-15 17:02:34

回答

16

用SMS-Delivery通知發送SMS作爲吐司。

方法調用如下。

sendSMS("98********","This is test message"); 

方法簽名如下。

/* 
* BroadcastReceiver mBrSend; BroadcastReceiver mBrReceive; 
*/ 
private void sendSMS(String phoneNumber, String message) { 
    ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>(); 
    ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>(); 
    PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, 
      new Intent(mContext, SmsSentReceiver.class), 0); 
    PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0, 
      new Intent(mContext, SmsDeliveredReceiver.class), 0); 
    try { 
     SmsManager sms = SmsManager.getDefault(); 
     ArrayList<String> mSMSMessage = sms.divideMessage(message); 
     for (int i = 0; i < mSMSMessage.size(); i++) { 
      sentPendingIntents.add(i, sentPI); 
      deliveredPendingIntents.add(i, deliveredPI); 
     } 
     sms.sendMultipartTextMessage(phoneNumber, null, mSMSMessage, 
       sentPendingIntents, deliveredPendingIntents); 

    } catch (Exception e) { 

     e.printStackTrace(); 
     Toast.makeText(getBaseContext(), "SMS sending failed...",Toast.LENGTH_SHORT).show(); 
    } 

} 

現在還有兩個類SmsDeliveredReceiver,SmsSentReceiver如下。

public class SmsDeliveredReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent arg1) { 
    switch (getResultCode()) { 
    case Activity.RESULT_OK: 
     Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show(); 
     break; 
    case Activity.RESULT_CANCELED: 
     Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show(); 
     break; 
    } 
} 

}

現在SMSSentReceiver。

public class SmsSentReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent arg1) { 
    switch (getResultCode()) { 
    case Activity.RESULT_OK: 
     Toast.makeText(context, "SMS Sent", Toast.LENGTH_SHORT).show(); 

     break; 
    case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
     Toast.makeText(context, "SMS generic failure", Toast.LENGTH_SHORT) 
       .show(); 

     break; 
    case SmsManager.RESULT_ERROR_NO_SERVICE: 
     Toast.makeText(context, "SMS no service", Toast.LENGTH_SHORT) 
       .show(); 

     break; 
    case SmsManager.RESULT_ERROR_NULL_PDU: 
     Toast.makeText(context, "SMS null PDU", Toast.LENGTH_SHORT).show(); 
     break; 
    case SmsManager.RESULT_ERROR_RADIO_OFF: 
     Toast.makeText(context, "SMS radio off", Toast.LENGTH_SHORT).show(); 
     break; 
    } 
} 

}

現在權限打開你的AndroidManifest.xml中添加以下行

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

和對其做.......

+0

什麼是這些廣播接收機在清單中的意圖過濾器? – Saty 2014-07-28 08:10:27

+0

@sandy我有嘗試,但它不工作 – 2016-05-31 17:30:05

+0

@Saty這裏是廣播接收器發送短信。 您可以在清單文件中定義它。 – GrIsHu 2016-08-17 11:34:10

2
+0

感謝您的鏈接。但文章沒有解釋我是否可以在後臺發送短信 – Zubair 2011-06-15 17:06:46

+0

您可以使用來自「Service」或「BroadcastReceiver」這個有效背景的邏輯(除非您對背景有不同的看法?)。如果你更具體地談論你想要做什麼? – Femi 2011-06-15 17:08:12

+0

您的意思是我們可以複製BroadcastReceiver或Service類中的代碼嗎? – Zubair 2011-06-15 17:15:03

16

是的,你也可以。

SmsManager sm = SmsManager.getDefault(); 
sm.sendTextMessage(number, null, message, null, null); 
+0

你有沒有試過這個。它是否從後臺工作? – Zubair 2011-06-15 17:14:12

+2

@Zubair我從來沒有親自嘗試過,其他人有,它的作品。我在你的問題評論中發送的鏈接提到它的工作。 – sealz 2011-06-15 17:16:31

+0

不要忘記這在清單中: <使用權限android:name =「android.permission.SEND_SMS」/> – 2016-02-22 23:33:46

0

最好的答案是好的,但上方API級別23您將需要務實地獲得權限。否則每次都會提示權限。

private static final int PERMISSION_REQUEST_CODE = 1; 

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { 

    if (checkSelfPermission(Manifest.permission.SEND_SMS) 
      == PackageManager.PERMISSION_DENIED) { 

     Log.d("permission", "permission denied to SEND_SMS - requesting it"); 
     String[] permissions = {Manifest.permission.SEND_SMS}; 

     requestPermissions(permissions, PERMISSION_REQUEST_CODE); 

    } 
}