SMS INBOX - LISTVIEW

时间:2011-02-04 10:35:32

标签: android

您好我正在尝试创建短信应用程序女巫在ListView中显示我的短信收件箱。但是如果我打开这个应用程序没有动作。我只能看到应用程序名称的黑屏:(。可以查看我的源代码和告诉我问题出在哪里?谢谢。

有源代码:

package sms.five;

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.telephony.SmsManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Smsfive extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState, ListView messages) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView list = (ListView) findViewById(R.id.list);

        List<String> msgList = getSMS();

        for(int i = 0; i<msgList.size(); i++) {
            System.out.println(msgList.get(i));
       }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.main, msgList); 
        list.setAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener()
       {
        @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
         SmsManager m = SmsManager.getDefault();
         String destinationNumber=(String) ((TextView)view).getText();              
         m.sendTextMessage(destinationNumber, null, destinationNumber, null, null);

       }          
         });
       }

    public List<String> getSMS() {      
        List<String> list = new ArrayList<String>();
        Uri uri = Uri.parse("content://sms/inbox");
        Cursor c = null;
        try{
            c = getApplicationContext().getContentResolver().query(uri, null, null ,null,null); 
        }catch(Exception e){
            e.printStackTrace();
        }
        try{
            for (boolean hasData = c.moveToFirst(); hasData; hasData = c.moveToNext()) {
                final String address = c.getString(c.getColumnIndex("address"));
                final String body = c.getString(c.getColumnIndexOrThrow("body"));
                list.add("Number: " + address + " .Message: " + body);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
     c.close(); 
     return list;
    }
}

XML文件(main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget30"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
</LinearLayout>

AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="sms.five"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Smsfive"
                  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>


<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
</manifest> 

3 个答案:

答案 0 :(得分:0)

我假设您的getSMS()没有返回任何内容。您可以在此行之后添加一些调试代码来检查:

List<String> msgList = getSMS();

这样的东西
Log.d("yourTag","number of items: ".msgList->size());

似乎它没有返回任何东西。您可以从调试该特定函数开始:是否有任何异常被捕获?添加一些日志记录来查看。也许你没有在清单中添加正确的行

 <uses-permission android:name="android.permission.READ_SMS"/>

或者该功能出现其他问题。你自己写的,还是副本?

答案 1 :(得分:0)

在你的getSMS方法中删除c.close()

答案 2 :(得分:0)

问题是行“ArrayAdapter adapter = new ArrayAdapter(this,R.layout.main,msgList);”

参数R.layout.main为false,它的布局项由您自定义或android构建的示例如android.R.layout.simple_list_item_1

=&GT;改变R.layout.main - &gt; android.R.layout.simple_list_item_1。 succsess 100%