您好,登錄后才能下訂單哦!
這篇文章主要講解了“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中如何實現短信編輯器功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。