Android 发送短信功能
2013-05-16 15:51
316 查看
1)关键代码如下
1.MainActivity.java
package com.example.androidtestapp;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button1=null;
private String phoneNumberString="10086";
private String msgString="10086";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button1.setText("send "+msgString+" to "+phoneNumberString);
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
/*建构一获得default instance的SmsManager对象*/
SmsManager smsManager=SmsManager.getDefault();
/*收件人电话号码格式与短信字数没有超过70个字符,发送短信*/
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(), 0);
smsManager.sendTextMessage(phoneNumberString, null , msgString, pendingIntent,null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
2.需要添加发送短信的权限
AndroidManifest.xml代码如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidtestapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidtestapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!--添加可以发送短信的权限 -->
<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>
1.MainActivity.java
package com.example.androidtestapp;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button1=null;
private String phoneNumberString="10086";
private String msgString="10086";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button1.setText("send "+msgString+" to "+phoneNumberString);
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
/*建构一获得default instance的SmsManager对象*/
SmsManager smsManager=SmsManager.getDefault();
/*收件人电话号码格式与短信字数没有超过70个字符,发送短信*/
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(), 0);
smsManager.sendTextMessage(phoneNumberString, null , msgString, pendingIntent,null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
2.需要添加发送短信的权限
AndroidManifest.xml代码如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidtestapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidtestapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!--添加可以发送短信的权限 -->
<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>
相关文章推荐
- Android----Intent,运用由android系统帮助匹配实现打电话、发送短信、打开网页、播放音乐、打开视频、打开图片、安装APK、通知栏消息、拍照上传头像等功能
- Android学习--多媒体功能:接收和发送短信
- Android实现发送短信验证码倒计时功能示例
- 脚本控制向Android模拟拨打电话,发送短信,定位设置功能
- android 发送短信功能
- android实现短信发送功能
- Android实现发送短信的功能
- android小功能实现之发送短信
- 02_android入门_实现短信/彩信发送的功能
- Android发送短信功能代码
- Android 学习第13课,android 实现发送短信的功能
- Android学习之1:实现发送短信功能
- android发送短信带监听是否发送成功功能
- Android中实现发送短信功能
- Android短信发送功能
- android 发送短信功能
- 举例说明android中SmsManager的用法---用来实现手机发送短信的功能
- Android实现拨打电话、发送短信和发送邮件功能
- Android实现短信发送功能
- Android实现发送短信功能实例详解