Android编程实现的短信编辑器功能示例

作者:woider 时间:2022-09-08 12:58:44 

本文实例讲述了Android编程实现的短信编辑器功能。分享给大家供大家参考,具体如下:

修改短信数据库,从而生成任意手机号发送的短信。

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.dudon.fakesms">
 <uses-permission android:name="android.permission.READ_SMS" />
 <uses-permission android:name="android.permission.WRITE_SMS" />
 <application
   android:allowBackup="true"
   android:icon="@mipmap/ic_launcher"
   android:label="@string/app_name"
   android:supportsRtl="true"
   android:theme="@style/AppTheme">
   <activity android:name=".MainActivity">
     <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
   </activity>
 </application>
</manifest>

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
   <TextView
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:gravity="center"
     android:text="短信发送者:"
     android:textSize="18sp" />
   <EditText
     android:id="@+id/get_phone"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_weight="7"
     android:inputType="phone" />
 </LinearLayout>
 <ScrollView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1">
   <EditText
     android:id="@+id/get_message"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_margin="20dp"
     android:hint="短信内容" />
 </ScrollView>
 <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
   <Button
     android:id="@+id/get_time"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:text="添加当前时间" />
   <Button
     android:id="@+id/send_message"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_weight="4"
     android:text="发送短信" />
 </LinearLayout>
</LinearLayout>

MainActivity.java


public class MainActivity extends AppCompatActivity {
 private int phoneNum;
 private String textSMS;
 private String currentTime;
 private Button sendMessage;
 private Button getTime;
 private EditText getPhone;
 private EditText getMessage;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   //注册控件
   sendMessage = (Button) findViewById(R.id.send_message);
   getTime = (Button) findViewById(R.id.get_time);
   getPhone = (EditText) findViewById(R.id.get_phone);
   getMessage = (EditText) findViewById(R.id.get_message);
   //获取当前时间
   getTime.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       textSMS = getMessage.getText().toString();
       SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
       Date curDate = new Date(System.currentTimeMillis());//获取当前时间
       currentTime = formatter.format(curDate);
       textSMS = textSMS + currentTime;
       getMessage.setText(textSMS);
     }
   });
   //发送短信
   sendMessage.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       if (TextUtils.isEmpty(getPhone.getText().toString())) {
         Toast.makeText(MainActivity.this, "电话号码未填写", Toast.LENGTH_SHORT).show();
         return;
       }
       if (TextUtils.isEmpty(getMessage.getText().toString())) {
         Toast.makeText(MainActivity.this, "短信内容未填写", Toast.LENGTH_SHORT).show();
         return;
       }
       //获取电话号码和短信内容
       phoneNum = Integer.parseInt(getPhone.getText().toString());
       textSMS = getMessage.getText().toString();
       //开启多线程
       Thread thread = new Thread() {
         @Override
         public void run() {
           ContentResolver resolver = getContentResolver();
           ContentValues values = new ContentValues();
           values.put("address", phoneNum);
           values.put("type", 1);
           values.put("date", System.currentTimeMillis());
           values.put("body", textSMS);
           resolver.insert(Uri.parse("content://sms"), values);
         }
       };
       thread.start();
       Toast.makeText(MainActivity.this, "短信成功生成", Toast.LENGTH_SHORT).show();
     }
   });
 }
}

运行截图:

Android编程实现的短信编辑器功能示例

Android编程实现的短信编辑器功能示例

希望本文所述对大家Android程序设计有所帮助。

来源:http://www.cnblogs.com/woider/p/5140972.html

标签:Android,短信
0
投稿

猜你喜欢

  • JavaEE微框架Spring Boot深入解读

    2021-07-31 14:24:55
  • Java之线程编程的4种方法实现案例讲解

    2021-11-18 18:40:46
  • C语言实现扫雷游戏(含注释详解)

    2023-11-02 15:25:07
  • Java 关于String字符串原理上的问题

    2021-05-26 12:48:44
  • Java 动态数组的实现示例

    2022-02-27 07:05:25
  • 8种常见的Java不规范代码

    2023-05-24 10:59:53
  • Java 散列存储详解及简单示例

    2022-06-30 23:19:52
  • Java协议字节操作工具类详情

    2023-11-02 06:25:02
  • java多线程编程之InheritableThreadLocal

    2022-02-08 21:40:32
  • 使用fastjson中的JSONPath处理json数据的方法

    2021-12-14 09:09:58
  • Java语言中4种内部类的超详细讲解

    2022-02-23 18:08:10
  • Android编程使用LinearLayout和PullRefreshView实现上下翻页功能的方法

    2023-06-30 02:16:15
  • JAVA遍历map的几种实现方法代码

    2023-08-27 22:06:04
  • Java NIO框架Netty简单使用的示例

    2022-09-08 02:51:27
  • java 集合----Map、Collection

    2022-11-09 03:39:01
  • Android四大组件之broadcast广播使用讲解

    2023-07-27 12:41:50
  • Android 5秒学会使用手势解锁功能

    2023-07-11 13:48:32
  • mybatis查询返回Map<String,Object>类型的讲解

    2022-12-25 02:07:38
  • 解决idea默认带的equals和hashcode引起的bug

    2023-12-22 19:02:43
  • 使用IDEA将Java/Kotliin工程导出Jar包的正确姿势

    2022-10-18 17:39:34
  • asp之家 软件编程 m.aspxhome.com