Android学习笔记_2_发送短信

1、首先需要在AndroidManifest.xml文件中加入发送短信的权限

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

2、使用相对布局方式进行布局

  hint:表示编辑框的提示信息

  layout_below:在那个视图的下方

<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=".MainActivity" >

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/tvPhone"

        android:id="@+id/tv1" />



    <EditText

        android:id="@+id/editPh"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/tv1"

        android:layout_below="@+id/tv1"

        android:hint="@string/phoneMsg"

        android:ems="10" />    

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/editPh"

        android:layout_below="@+id/editPh"

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

    <EditText

        android:id="@+id/etSMS"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/textView1"

        android:layout_below="@+id/textView1"

        android:ems="10" />

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/etSMS"

        android:layout_below="@+id/etSMS"

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

</RelativeLayout>

3、string的资源文件

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

<resources>

    <string name="app_name">SMS</string>

    <string name="action_settings">Settings</string>

    <string name="tvPhone">请输入手机号</string>

    <string name="tvSMS">请输入短信内容</string>

    <string name="success">发送成功</string>

    <string name="fail">发送失败</string>

    <string name="phoneMsg">Please a phone number</string>

    <string name="send">发送</string>

</resources>

4、activity类的实现

public class MainActivity extends Activity {

    private EditText etPhone;

    private EditText etSMS;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        etPhone = (EditText)this.findViewById(R.id.editPh);//号码

        etSMS = (EditText)this.findViewById(R.id.etSMS);//短信内容

        Button btnSend = (Button)this.findViewById(R.id.button1);

        //注册事件

        btnSend.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View view) {

                String phone = etPhone.getText().toString();

                String sms = etSMS.getText().toString();

                SmsManager manager = SmsManager.getDefault();

                //如果短信太长,就分割开来在进行发送

                ArrayList<String> msgs = manager.divideMessage(sms);

                for (String msg : msgs) {

                    manager.sendTextMessage(phone, null, msg, null, null);

                }

                //参数分别表示上下文对象,显示消息,停留时间长短

                Toast.makeText(MainActivity.this, R.string.success, 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.main, menu);

        return true;

    }



}

 

你可能感兴趣的:(Android学习)