我想从收件箱中读取所有短信,并在列表视图中显示这些短信。
我面临的问题是:
1)我得到List“msgList”为空白,List“labeles”[来自方法getSms()]为空白。我在收件箱中有2个短信。
2)它永远不会在for循环中打印hhhhhhh + labeles。
有人可以建议我更改以下代码,以便我可以读取收件箱短信并将其列入listView吗?我错在哪里?
我写了以下代码: ViewTask.java
package com.example.myapp;
import java.util.ArrayList;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ViewTask extends Activity {
TextView tv_view_task;
ListView lv_view_task;
static String sms = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_task);
tv_view_task=(TextView) findViewById(R.id.tv_view_task);
lv_view_task=(ListView) findViewById(R.id.lv_view_task);
List<String> msgList = getSms();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, msgList);
lv_view_task.setAdapter(adapter);
}
public List<String> getSms()
{
List<String> labeles = new ArrayList<String>();
Uri uri = Uri.parse("content://sms/inbox");
Cursor c= getContentResolver().query(uri, null, null ,null,null);
// Read the sms data and store it in the list
if(c.moveToFirst()) {
for(int i=0; i < c.getCount(); i++) {
sms += "From :" + c.getString(2) + " : " + c.getString(11)+"\n";
labeles.add(sms);
System.out.println("hhhhhh"+labeles);
c.moveToNext();
}
}
c.close();
System.out.println("aaaaaaa"+labeles);
return labeles;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_view_task, menu);
return true;
}
}
这是我的activity_view_task.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Tasks"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/lv_view_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_tasks"
android:layout_centerHorizontal="true" >
</ListView>
的Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_SMS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMsg"
android:label="@string/title_activity_display_msg" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name=".FormActivity"
android:label="@string/title_activity_form" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name=".DetailsActivity"
android:label="@string/title_activity_details" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name=".MainPageActivity"
android:label="@string/title_activity_main_page" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ViewTaskActivity"
android:label="@string/title_activity_view_task" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name=".ViewTask"
android:label="@string/title_activity_view_task" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
答案 0 :(得分:1)
尝试这种方法..
public List<String> getSms() {
List<String> labeles = new ArrayList<String>();
Uri uri = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uri, null, null, null, null);
String sms = null;
// Read the sms data and store it in the list
if (c.moveToFirst()) {
do {
String body = c.getString(c.getColumnIndex("body"));
String address = c.getString(c.getColumnIndex("address"));
sms = "From : " + address + " : " + body;
labeles.add(sms);
} while (c.moveToNext());
}
c.close();
// System.out.println("aaaaaaa" + labeles);
return labeles;
}
注意:别忘了添加权限..
<uses-permission android:name="android.permission.READ_SMS"/>
答案 1 :(得分:0)
我在代码中做了一些更改尝试这个
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ViewTask extends Activity {
TextView tv_view_task;
ListView lv_view_task;
static String sms = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_task);
tv_view_task=(TextView) findViewById(R.id.tv_tasks);
lv_view_task=(ListView) findViewById(R.id.lv_view_task);
List<String> msgList = getSms();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, msgList);
lv_view_task.setAdapter(adapter);
}
public List<String> getSms()
{
List<String> labeles = new ArrayList<String>();
Uri uri = Uri.parse("content://sms/inbox");
Cursor c= getContentResolver().query(uri, null, null ,null,null);
// Read the sms data and store it in the list
if(c.moveToFirst()) {
for(int i=0; i < c.getCount(); i++) {
sms += "From :" + c.getString(2) + " : " + c.getString(11)+"\n";
labeles.add(sms);
Log.i("meenal","hhhhhh"+labeles);
c.moveToNext();
}
}
c.close();
Log.i("meenal","aaaaaaa"+labeles);
return labeles;
}
}
和你的xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/tv_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Tasks"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/lv_view_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_tasks"
android:layout_centerHorizontal="true" >
</ListView>
并在清单
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.imaegcolourchange.ViewTask"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>