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

 更新时间:2017年08月10日 10:19:28   作者:woider  
这篇文章主要介绍了Android编程实现的短信编辑器功能,涉及Android权限控制、界面布局及短信功能相关操作技巧,需要的朋友可以参考下

本文实例讲述了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文件操作技巧汇总》、《Android编程之activity操作技巧总结》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结

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

相关文章

  • 浅谈Android添加快捷方式ShortCut

    浅谈Android添加快捷方式ShortCut

    这篇文章主要介绍了浅谈Android添加快捷方式ShortCut,对添加快捷方式感兴趣的同学,可以参考下
    2021-04-04
  • Android开发悬浮按钮 Floating ActionButton的实现方法

    Android开发悬浮按钮 Floating ActionButton的实现方法

    这篇文章主要介绍了Android开发悬浮按钮 Floating ActionButton的实现方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Android仿ViVO X6 极速闪充动画效果

    Android仿ViVO X6 极速闪充动画效果

    这篇文章主要介绍了Android仿ViVO X6 极速闪充动画效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android编程之简单启动画面实现方法

    Android编程之简单启动画面实现方法

    这篇文章主要介绍了Android编程之简单启动画面实现方法,结合实例形式较为详细的分析了开机启动画面的制作步骤及布局、Activity跳转、权限控制等的相关操作技巧,需要的朋友可以参考下
    2016-11-11
  • Android快速实现触摸移动的悬浮窗

    Android快速实现触摸移动的悬浮窗

    这篇文章主要为大家详细介绍了Android快速实现触摸移动的悬浮窗,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Android图像处理之泛洪填充算法

    Android图像处理之泛洪填充算法

    这篇文章主要介绍了泛洪填充算法,工作原理是从一个点开始附近像素点,填充成新的颜色,直到封闭区域内的所有像素点都被填充新颜色为止,分享给大家供大家参考
    2018-05-05
  • Android开发之保存图片到相册的三种方法详解

    Android开发之保存图片到相册的三种方法详解

    这篇文章主要介绍了Android开发实现的保存图片到相册功能的三种方法,文中的示例代码讲解详细,有一定的参考价值,感兴趣的可以了解一下
    2022-04-04
  • Android SeekBar在刷新使用中需要注意的问题

    Android SeekBar在刷新使用中需要注意的问题

    SeekBar在刷新使用中需要注意的问题:在使用SeekBar的过程中需要注意刷新频率,避免频繁刷新造成的性能问题;同时,需要对SeekBar的监听事件进行适当的优化,减少回调次数,提高响应速度
    2023-05-05
  • Android Eclipse 注释模板的使用(图文说明)

    Android Eclipse 注释模板的使用(图文说明)

    为提高代码的可读性以及后期的可维护性,为我们的代码加上规范化的注释是很有必要,不仅有利于提高自己的专业素养,也能方便他人
    2013-12-12
  • Android数据持久化之ContentProvider机制详解

    Android数据持久化之ContentProvider机制详解

    这篇文章主要介绍了Android数据持久化之ContentProvider机制,结合实例形式分析了ContentProvider机制的原理与相关使用技巧,需要的朋友可以参考下
    2017-05-05

最新评论