註冊 登錄
Android 台灣中文網 返回首頁

fchien的個人空間 https://www.apk.tw/?1412 [收藏] [複製] [分享] [RSS]

日誌

SMS with broadcast

已有 1106 次閱讀2011-5-10 10:25 |個人分類:程式

package com.android.myBroadcastTest;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;

public class myBroadcastTest extends BroadcastReceiver {

    private final String MSG_LOG = "REC_LOG";
    
    @Override
    public void onReceive(Context context, Intent intent) {

        ArrayList<String> divideMsg = null;
        Log.d(MSG_LOG, "onReceive");
        Bundle bundle = intent.getExtras();
        if(bundle != null)
        {
            String strPhone = bundle.get("PhoneNumber").toString();
            String strMsg = bundle.get("SMSMessage").toString();
            Log.d(MSG_LOG, strPhone);
            Log.d(MSG_LOG, strMsg);

            SmsManager smsManager = SmsManager.getDefault();
            PendingIntent mPI = PendingIntent.getBroadcast(context, 0, new Intent(), 0);
            //smsManager.sendTextMessage(strPhone, null, strMsg, mPI, null);
            divideMsg = smsManager.divideMessage(strMsg);
            Iterator<String> iterator = divideMsg.iterator();
            while(iterator.hasNext()){
                String strTmp = iterator.next();
                smsManager.sendTextMessage(strPhone, null, strTmp, mPI, null);
            }
        }
        abortBroadcast();
    }

}


AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.myBroadcastTest" android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon"
        android:label="@string/app_name">
        <receiver
            android:name=".myBroadcastTest">
            <intent-filter>
                <action android:name="mySendSMS" />
            </intent-filter>
        </receiver>

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

use adb command
adb shell am broadcast -a mySendSMS --es "0123455667" --es "SMS message send test"

路過

雞蛋

鮮花

握手

雷人

評論 (0 個評論)

facelist

您需要登錄後才可以評論 登錄 | 註冊