短信收件箱 - LISTVIEW [英] SMS INBOX - LISTVIEW
问题描述
您好我想创建短信aplication女巫显示我的短信收件箱中ListView.But如果我打开这个aplication没有action.I只能看到黑屏与应用名称:(。可有人检查我的源code,告诉我问题出在哪里?谢谢。
有源$ C $ C:
包sms.five;
进口的java.util.ArrayList;
进口的java.util.List;
进口android.app.Activity;
进口android.database.Cursor;
进口android.net.Uri;
进口android.os.Bundle;
进口android.telephony.SmsManager;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.TextView;
公共类Smsfive延伸活动{
/ **第一次创建活动时调用。 * /
公共无效的onCreate(捆绑savedInstanceState,ListView的消息){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
ListView控件列表=(ListView控件)findViewById(R.id.list);
名单<字符串> msgList = getSMS();
的for(int i = 0; I< msgList.size();我++){
的System.out.println(msgList.get(一));
}
ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,R.layout.main,msgList);
list.setAdapter(适配器);
list.setOnItemClickListener(新OnItemClickListener()
{
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
SmsManager的米= SmsManager.getDefault();
字符串destinationNumber =(字符串)((TextView中)查看).getText();
m.sendTextMessage(destinationNumber,空,destinationNumber,NULL,NULL);
}
});
}
公开名单<字符串> getSMS(){
名单<字符串>名单=新的ArrayList<字符串>();
开放的我们的uri = Uri.parse(内容://短信/收件箱);
光标C = NULL;
尝试{
C = getApplicationContext()getContentResolver()查询(URI,NULL,NULL,NULL,NULL);
}赶上(例外五){
e.printStackTrace();
}
尝试{
为(布尔hasData = c.moveToFirst(); hasData; hasData = c.moveToNext()){
最终的字符串地址= c.getString(c.getColumnIndex(地址));
最后弦乐体= c.getString(c.getColumnIndexOrThrow(身体));
list.add(号码:+地址+.Message:+体);
}
}赶上(例外五){
e.printStackTrace();
}
c.close();
返回列表;
}
}
XML文件(main.xml中):
< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
机器人:ID =@ + ID / widget30
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
的xmlns:机器人=http://schemas.android.com/apk/res/android
>
<的ListView
机器人:ID =@ + ID /列表
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
< / ListView控件>
< / LinearLayout中>
AndroidManifest.xml文件:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=sms.five
安卓版code =1
机器人:VERSIONNAME =1.0>
<应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>
<活动机器人:SmsfiveNAME =
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>
<使用-权限的Android:名称=android.permission.READ_SMS>< /使用-许可>
< /舱单>
我假设你的 getSMS()
不返回任何东西。您可以通过此行之后加入一些调试code检查:
名单,其中,字符串> msgList = getSMS();
类似于
Log.d(yourTag,项目数:.msgList->尺寸());
看来,它不返回任何东西。有没有抓到任何例外:您可以通过调试特定功能的开始?添加一些记录一看就知道。也许你没有添加正确的线路在你的清单
<使用-权限的Android:名称=android.permission.READ_SMS/>
还是其他什么东西出了问题该函数。你把它写自己,或者是它的一个副本?
Hello i am trying to create SMS aplication witch shows me SMS Inbox in ListView.But if i open this aplication there is no action.I can see only black screen with name of aplication :( .Can somebody check my source code and tell me where is the problem? Thank you.
There is source code:
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 File(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 file:
<?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>
I'm assuming your getSMS()
is not returning anything. You can check this by adding some debugging code after this line:
List<String> msgList = getSMS();
Something like
Log.d("yourTag","number of items: ".msgList->size());
It seems that it doesn't return anything. You might start by debugging that particular function: are there any exceptions caught? add some logging to see that. Maybe you didn't add the correct line in your manifest
<uses-permission android:name="android.permission.READ_SMS"/>
Or something else goes wrong in that function. Did you write it yourself, or was it a copy?
这篇关于短信收件箱 - LISTVIEW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!