15

私は、ユーザーに送信の確認を求めずにバックグラウンドで SMS を送信できない iPhone 開発から来ています。ユーザーの介入が不要になるように、Android のバックグラウンドで SMS を送信できますか?

4

4 に答える 4

28

Send SMS with SMS-Delivery notification as toast.

method call as below.

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

method signature as below.

/*
 * 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();
    }

}

Now two more classes SmsDeliveredReceiver,SmsSentReceiver as below.

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

Now 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;
        }
    }
}

Now Permissions open your AndroidManifest.xml and add below line

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

and its done.......

于 2012-11-21T10:17:54.413 に答える
19

はい、次を使用して実行できます。

SmsManager sm = SmsManager.getDefault(); 
sm.sendTextMessage(number, null, message, null, null); 
于 2011-06-15T17:10:34.473 に答える
2

http://thinkandroid.wordpress.com/2010/01/08/sending-sms-from-application/を参照してください。適切な権限が必要です。

于 2011-06-15T17:03:20.387 に答える