溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android中怎么實(shí)現(xiàn)短信編輯器功能

發(fā)布時(shí)間:2021-06-26 16:57:08 來源:億速云 閱讀:195 作者:Leah 欄目:移動(dòng)開發(fā)

Android中怎么實(shí)現(xiàn)短信編輯器功能,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

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="短信發(fā)送者:"
      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="短信內(nèi)容" />
  </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="添加當(dāng)前時(shí)間" />
    <Button
      android:id="@+id/send_message"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_weight="4"
      android:text="發(fā)送短信" />
  </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);
    //注冊(cè)控件
    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);
    //獲取當(dāng)前時(shí)間
    getTime.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        textSMS = getMessage.getText().toString();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒");
        Date curDate = new Date(System.currentTimeMillis());//獲取當(dāng)前時(shí)間
        currentTime = formatter.format(curDate);
        textSMS = textSMS + currentTime;
        getMessage.setText(textSMS);
      }
    });
    //發(fā)送短信
    sendMessage.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (TextUtils.isEmpty(getPhone.getText().toString())) {
          Toast.makeText(MainActivity.this, "電話號(hào)碼未填寫", Toast.LENGTH_SHORT).show();
          return;
        }
        if (TextUtils.isEmpty(getMessage.getText().toString())) {
          Toast.makeText(MainActivity.this, "短信內(nèi)容未填寫", Toast.LENGTH_SHORT).show();
          return;
        }
        //獲取電話號(hào)碼和短信內(nèi)容
        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();
      }
    });
  }
}

看完上述內(nèi)容,你們掌握Android中怎么實(shí)現(xiàn)短信編輯器功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI