• 短信发送器


    一、界面

    1.最终要实现的界面:

    2.界面布局实现——activity_main.xml:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:orientation="vertical"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent" >
     5 
     6     <TextView
     7         android:layout_width="match_parent"
     8         android:layout_height="wrap_content"
     9         android:text="@string/mobile" />
    10     
    11     <EditText 
    12         android:id="@+id/editMobile"
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         />
    16     
    17     <TextView
    18         android:layout_width="match_parent"
    19         android:layout_height="wrap_content"
    20         android:text="@string/sms" />
    21     
    22     <EditText 
    23         android:id="@+id/editSms"
    24         android:layout_width="match_parent"
    25         android:layout_height="wrap_content"
    26         android:minLines="3"
    27         />
    28 
    29     <Button 
    30         android:id="@+id/button"
    31         android:layout_width="wrap_content"
    32         android:layout_height="wrap_content"
    33         android:text="@string/button"/>
    34 </LinearLayout>

    string.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <resources>
     3 
     4     <string name="app_name">短信发送器</string>
     5     <string name="mobile">请输入手机号</string>
     6     <string name="sms">请输入短信内容</string>
     7     <string name="button">发送短信</string>
     8     <string name="success">发送完成</string>
     9     <string name="hello_world">Hello world!</string>
    10     <string name="action_settings">Settings</string>
    11 
    12 </resources>

    二、权限

    清单文件中申请权限:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.example.sms"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk
     8         android:minSdkVersion="14"
     9         android:targetSdkVersion="21" />
    10 
    11     <application
    12         android:allowBackup="true"
    13         android:icon="@drawable/ic_launcher"
    14         android:label="@string/app_name"
    15         android:theme="@style/AppTheme" >
    16         <activity
    17             android:name=".MainActivity"
    18             android:label="@string/app_name" >
    19             <intent-filter>
    20                 <action android:name="android.intent.action.MAIN" />
    21 
    22                 <category android:name="android.intent.category.LAUNCHER" />
    23             </intent-filter>
    24         </activity>
    25     </application>
    26 
    27     <uses-permission android:name="android.permission.SEND_SMS" />
    28     
    29 </manifest>

    三、代码实现

    MainActivity.java:

     1 package com.example.sms;
     2 
     3 import java.util.ArrayList;
     4 
     5 import android.app.Activity;
     6 import android.content.Intent;
     7 import android.os.Bundle;
     8 import android.telephony.gsm.SmsManager;
     9 import android.text.InputFilter.LengthFilter;
    10 import android.view.Menu;
    11 import android.view.MenuItem;
    12 import android.view.View;
    13 import android.widget.Button;
    14 import android.widget.EditText;
    15 import android.widget.Toast;
    16 
    17 public class MainActivity extends Activity {
    18     private EditText editMobile;
    19     private EditText editSms;
    20     
    21     @Override
    22     protected void onCreate(Bundle savedInstanceState) {
    23         super.onCreate(savedInstanceState);
    24         setContentView(R.layout.activity_main);
    25         
    26         Button button = (Button)findViewById(R.id.button);
    27         button.setOnClickListener(new onButtonClickListener());
    28         
    29         editMobile= (EditText)findViewById(R.id.editMobile);
    30         editSms= (EditText)findViewById(R.id.editSms);
    31     }
    32 
    33     private final class onButtonClickListener implements View.OnClickListener
    34     {
    35 
    36         @Override
    37         public void onClick(View v) {
    38             // TODO Auto-generated method stub
    39             String mobileText = editMobile.getText().toString();
    40             String smsText = editSms.getText().toString();
    41             SmsManager manager = SmsManager.getDefault();
    42             ArrayList<String> texts = manager.divideMessage(smsText);
    43             for(String text : texts)//采用增强for循环
    44             {
    45                 manager.sendTextMessage(mobileText, null, text, null, null);
    46             }
    47             //提示用户短信发送完成,提示有3中方式
    48             //通知:1.状态栏通知     2.对话框通知:给用户的感觉比较生硬    3.吐司(Toast):比较柔和
    49     //        Toast.makeText(getApplicationContext(), R.string.success, 0);//
    50             Toast.makeText(MainActivity.this, R.string.success,Toast.LENGTH_SHORT).show();
    51         }
    52         
    53     }
    54 }

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent" >
        <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/mobile" />        <EditText         android:id="@+id/editMobile"        android:layout_width="match_parent"        android:layout_height="wrap_content"        />        <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/sms" />        <EditText         android:id="@+id/editSms"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:minLines="3"        />
        <Button         android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button"/></LinearLayout>

  • 相关阅读:
    DAY09(for语句)
    Python 下载 tushare 数据,然后调用 C++ DLL 计算 wMA 存入本地 csv 文件再 python 读取
    Python socket编程之八:阶段性总结
    Ctex + Lyx = 自用 Latex 编辑环境
    matlab下二重积分的蒙特卡洛算法
    实验9 根据材料编程
    实验5 编写、调试具有多个段的程序
    实验4 [bx]和loop的使用
    实验3 编程、编译、连接、跟踪
    汇编语言第三章总结
  • 原文地址:https://www.cnblogs.com/ttzm/p/7224139.html
Copyright © 2020-2023  润新知