这是我的主要java文件,我用sms内容提供程序获取短信并希望在我的应用程序中显示。但由于某些原因,应用程序明星没有任何错误,但它显示我的手机上的消息。它显示了所有按钮和列表视图,一切正确,但它应该显示的短信内容不存在。
package com.smsapp;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements OnClickListener {
// GUI Widget
Button btnSent, btnInbox, btnDraft;
TextView lblMsg, lblNo;
ListView lvMsg;
// Cursor Adapter
SimpleCursorAdapter adapter;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Init GUI Widget
btnInbox = (Button) findViewById(R.id.btnInbox);
btnInbox.setOnClickListener(this);
btnSent = (Button) findViewById(R.id.btnSentBox);
btnSent.setOnClickListener(this);
btnDraft = (Button) findViewById(R.id.btnDraft);
btnDraft.setOnClickListener(this);
lvMsg = (ListView) findViewById(R.id.lvMsg);
}
@Override
public void onClick(View v) {
if (v == btnInbox) {
// Create Inbox box URI
Uri inboxURI = Uri.parse("content://sms/inbox");
// List required columns
String[] reqCols = new String[]{"_id", "address", "body"};
// Get Content Resolver object, which will deal with Content
// Provider
ContentResolver cr = getContentResolver();
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber});
lvMsg.setAdapter(adapter);
}
if (v == btnSent) {
// Create Sent box URI
Uri sentURI = Uri.parse("content://sms/sent");
// List required columns
String[] reqCols = new String[]{"_id", "address", "body"};
// Get Content Resolver object, which will deal with Content
// Provider
ContentResolver cr = getContentResolver();
// Fetch Sent SMS Message from Built-in Content Provider
Cursor c = cr.query(sentURI, reqCols, null, null, null);
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber});
lvMsg.setAdapter(adapter);
}
if (v == btnDraft) {
// Create Draft box URI
Uri draftURI = Uri.parse("content://sms/draft");
// List required columns
String[] reqCols = new String[]{"_id", "address", "body"};
// Get Content Resolver object, which will deal with Content
// Provider
ContentResolver cr = getContentResolver();
// Fetch Sent SMS Message from Built-in Content Provider
Cursor c = cr.query(draftURI, reqCols, null, null, null);
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber});
lvMsg.setAdapter(adapter);
}
}
}
//这是我的第一个XML文件activity_main:
<?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:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<Button
android:id="@+id/btnInbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Inbox"></Button>
<Button
android:id="@+id/btnSentBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Sent Box"></Button>
<Button
android:id="@+id/btnDraft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Draft"></Button>
</LinearLayout>
<ListView
android:id="@+id/lvMsg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></ListView>
</LinearLayout>
//这是我的第二个XML文件行:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:textAppearance="?android:attr/textAppearanceLarge"
android:text="TextView" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/lblMsg"> </TextView>
<TextView android:textAppearance="?android:attr/textAppearanceMedium"
android:text="TextView" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#00f"
android:id="@+id/lblNumber"></TextView>