您的位置:首页 > 移动开发 > Android开发

Android通过SmsManager发送短信(附源码)

2013-11-06 10:53 591 查看
最近有个需求,需要在不调用短信界面的情况下发送短信,现将demo列出,供有需要的朋友查看。

配置文件AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.myhc.mms"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

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

<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

<activity

android:name=".SentMmsActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>



<receiver android:name=".SendReceiver" >

<intent-filter>

<action android:name="myhc.sendBroadCastSuccess" />

</intent-filter>

</receiver>

<receiver android:name=".AcceptReceiver">

<intent-filter>

<action android:name="myhc.acceptBroadCastSuccess" />

</intent-filter>

</receiver>

</application>

</manifest>

布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<Button

android:id="@+id/send"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/send" />

</LinearLayout>



短信发送文件SentMmsActivity

public class SentMmsActivity extends Activity {



@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button send = (Button) findViewById(R.id.send);

send.setOnClickListener(new View.OnClickListener() {

PendingIntent sentIntent = PendingIntent.getBroadcast(

SentMmsActivity.this, 0, new Intent("myhc.sendBroadCastSuccess"), 0);

PendingIntent receiverIntent = PendingIntent.getBroadcast(

SentMmsActivity.this, 0, new Intent("myhc.acceptBroadCastSuccess"), 0);

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

SmsManager smsManager = SmsManager.getDefault();

smsManager.sendTextMessage("18825080059", null, "你好",

sentIntent, receiverIntent);

}

});

}

}

接收发送成功信息文件SendReceiver

public class SendReceiver extends BroadcastReceiver {



@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

if (intent.getAction().equals("myhc.sendBroadCastSuccess")) {

Toast.makeText(context, "Send Success", Toast.LENGTH_SHORT).show();

}

}

}

接收对发接收成功的文件AcceptReceiver

public class AcceptReceiver extends BroadcastReceiver {



@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

if (intent.getAction().equals("myhc.acceptBroadCastSuccess")) {

Toast.makeText(context, "Accept Success", Toast.LENGTH_SHORT).show();

}

}

}



这个demo非常简单,在此我只说一下几点

PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出去。

函数SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中参数解释:

1.destinationAddress: 收件人号码

  2.scAddress: 短信中心服务号码, 这里设置为null

  3.text: 发送内容

  4.sentIntent: 发送短信结果状态信号(是否成功发送),new 一个Intent , 操作系统接收到信号后将广播这个Intent.此过程为异步.当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过 android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;

  5.deliveryIntent: 对方接收状态信号(是否已成功接收).是当消息已经传递给收信人后所进行的PendingIntent广播。



注意在AndroidMainFest.xml中使用

<uses-permission android:name="android.permission.SEND_SMS" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: