Android 趣味應用—— 簡訊編輯器

來源:互聯網
上載者:User

標籤:

修改簡訊資料庫,從而產生任意手機號發送的簡訊。

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 趣味應用—— 簡訊編輯器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.