• 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;
        }
    
    }
  • 相关阅读:
    Ceph14.2.5 RBD块存储的实战配置和详细介绍,不看后悔! -- <3>
    常见SQL命令总结学习 -- <1>
    全网最详细的新手入门Mysql命令和基础,小白必看!
    全网最详细的Linux命令系列-nl命令
    全网最详细的Linux命令系列-cat命令
    全网最详细的Linux命令系列-touch命令
    全网最详细的Ceph14.2.5集群部署及配置文件详解,快来看看吧! -- <2>
    什么是Ceph存储?什么是分布式存储?简单明了带你学Ceph -- <1>
    一款专注于阅读的博客园主题-(cnblogs-theme-silence)
    Prometheus 配置文件中 metric_relabel_configs 配置--转载
  • 原文地址:https://www.cnblogs.com/lbangel/p/3393399.html
Copyright © 2020-2023  润新知