獲取短信內容,講解在代碼中:
先來看佈局文件:
activity_main.xml;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="readSms"
android:text="顯示短信" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="insertSms"
android:text="插入短信" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
爲listView的item 設置佈局文件,這裏簡單的看一下:
sms.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" >
<TextView
android:id="@+id/addr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
主要代碼:
MianActivity.java:
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity {
private ListView listview;
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
listview = (ListView) findViewById(R.id.lv);
adapter = new SimpleCursorAdapter(this, R.layout.sms, null,
new String[] { "address", "body" }, new int[] { R.id.addr,
R.id.body },
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listview.setAdapter(adapter);
}
//插入短信
public void insertSms(View view) {
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://sms/inbox");
ContentValues values = new ContentValues();
values.put("address", "123456");
values.put("body", "收入10,00,000");
values.put("date", System.currentTimeMillis());
resolver.insert(uri, values);
}
//讀取短信
public void readSms(View view) {
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://sms");
Cursor cursor = resolver.query(uri, new String[] { "address", "body",
"_id" }, null, null, null);
adapter.changeCursor(cursor);
/*
* while (cursor != null && cursor.moveToNext()) { String address =
* cursor.getString(cursor.getColumnIndex("address")); String body =
* cursor.getString(cursor.getColumnIndex("body")); Log.e("aaa", address
* + " " + body);
*
* }
*/
}
}
//權限:讀取短信的權限和寫入短線的權限
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
獲取手機短信內容
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.