获取短信内容,讲解在代码中:
先来看布局文件:
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"/>
获取手机短信内容
最新推荐文章于 2024-08-22 02:18:40 发布
802

被折叠的 条评论
为什么被折叠?



