首先activity_main.xml 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SmsActivity" >

<TextView
android:id="@+id/phone_lable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:text="@string/phon_lable" />


<EditText
android:id="@+id/edit_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/phone_lable"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="phone" >

</EditText>

<TextView
android:id="@+id/sms_lable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/edit_phone"
android:layout_below="@+id/edit_phone"
android:layout_marginTop="22dp"
android:text="@string/sms_lable" />


<Button
android:id="@+id/But_sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/edit_sms"
android:layout_alignParentBottom="true"
android:layout_marginBottom="14dp"
android:text="@string/But_sms" />


<EditText
android:id="@+id/edit_sms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/But_sms"
android:layout_alignLeft="@+id/sms_lable"
android:ems="10"
android:inputType="textMultiLine" />

</RelativeLayout>



然后在AndroidManifest.xml 中添加 发送短信的权限:

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


接下来就是SmsActivity 中 主要代码 的实现 其功能 :


public class SmsActivity extends Activity {


// 声明控件对象
private Button butten_sms;
private EditText edit_phone;
private EditText edit_sms;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//设置显示视图
setContentView(R.layout.activity_sms);

//获取按钮组件
butten_sms=(Button) findViewById(R.id.But_sms);

//获取输入框组件
edit_phone = (EditText) findViewById(R.id.edit_phone);

edit_sms = (EditText) findViewById(R.id.edit_sms);

//注册按钮事件
butten_sms.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
/**
* 发彩信
*/
//获取电话号
String phone_num = edit_phone.getText().toString();

//创建意图对象
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/a.jpg"));
intent.putExtra("address", phone_num);
intent.putExtra("exit_on_sent", true);
intent.putExtra("subject", "subject:I love you");
intent.putExtra("sms_body", "content ::XXXX");
intent.setType("image/jpeg");


startActivity(intent);

/*// 获取短信管理器对象
SmsManager smsManager = SmsManager.getDefault();
// 意图对象
PendingIntent pendingIntent = PendingIntent.getBroadcast(
SmsActivity.this, 0, new Intent(), 0);*/



}
});
}


//发短信 功能
public void send(){
//获取号码
String phone_Num = edit_phone.getText().toString();

//获取发送的短信
String phone_sms = edit_sms.getText().toString();

//获取信息管理器对象
SmsManager smsmanager = SmsManager.getDefault();

//意图对象
PendingIntent pendingIntent = PendingIntent.getBroadcast(SmsActivity.this, 0, new Intent(), 0);

//判断 信息长度 大于 70则分两条发送。。默认是70
ArrayList<String> contents = smsmanager
.divideMessage(phone_sms);
for (String c : contents) {
// 发送信息的操作
smsmanager.sendTextMessage(phone_Num, null, c,
pendingIntent, null);
}

/*//发送信息操作
smsmanager.sendTextMessage(phone_Num, null, phone_sms, pendingIntent, null);*/

//吐司的效果
Toast.makeText(SmsActivity.this, "短信发送成功",Toast.LENGTH_LONG).show();
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sms, menu);
return true;
}

}



更多相关文章

  1. android获得屏幕高度和宽度
  2. 总结系列-Android的文件系统
  3. android设备信息获取
  4. Android(安卓)获取系统权限的代码
  5. 获取Android版本信息和电话信息
  6. FregServer进程,封装进程间通信数据
  7. Android(安卓)根据IP地址获取城市
  8. Android(安卓)铃声和音量的获取程序
  9. Android(安卓)获取经纬度。2018年写

随机推荐

  1. Android(安卓)屏蔽Power键 Home键
  2. 开发者必看|Android(安卓)8.0 新特性及开
  3. android内存优化
  4. 【23】Android(安卓)应用程序入口探究
  5. 友坚4412开发板Android振动器系统构架的
  6. android 在init.rc中配置socket及其使用
  7. Android(安卓)设备root 原理及方法
  8. Android学习笔记(8) - GridView与ImageVi
  9. 【平台开发】-Android使用MD5加密算法加
  10. 日积月累--exception记录