问题
Hi I am developing android SMS app where I am trying to retrieve messages from built in messaging app and display it on a listview
. I am using the below code for retrieving conversations.
Uri uriSMSURI = Uri.parse("content://mms-sms/conversations/");
This is working fine on all phones except Samsung Grand. I am getting exception as
08-23 11:29:53.778: E/AndroidRuntime(22826): FATAL EXCEPTION: main
08-23 11:29:53.778: E/AndroidRuntime(22826): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.MyApp/com.example.MyApp.MainActivity}:java.lang.NullPointerException
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.os.Looper.loop(Looper.java:137)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.app.ActivityThread.main(ActivityThread.java:4935)
08-23 11:29:53.778: E/AndroidRuntime(22826): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 11:29:53.778: E/AndroidRuntime(22826): at java.lang.reflect.Method.invoke(Method.java:511)
08-23 11:29:53.778: E/AndroidRuntime(22826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
08-23 11:29:53.778: E/AndroidRuntime(22826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
08-23 11:29:53.778: E/AndroidRuntime(22826): at dalvik.system.NativeStart.main(Native Method)
08-23 11:29:53.778: E/AndroidRuntime(22826):Caused by: java.lang.NullPointerException
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.os.Parcel.readException(Parcel.java:1431)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.content.ContentResolver.query(ContentResolver.java:372)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.content.ContentResolver.query(ContentResolver.java:315)
08-23 11:29:53.778: E/AndroidRuntime(22826): at com.example.MyApp.MainActivity.getSMS(MainActivity.java:188)
08-23 11:29:53.778: E/AndroidRuntime(22826): at com.example.MyApp.MainActivity.onCreate(MainActivity.java:64)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.app.Activity.performCreate(Activity.java:5206) 08-23
11:29:53.778: E/AndroidRuntime(22826): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-23 11:29:53.778: E/AndroidRuntime(22826): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-23 11:29:53.778: E/AndroidRuntime(22826): ... 11 more
This is my MainActivity.java
public class MainActivity extends Activity implements OnClickListener
{
private ImageView Msg;
private ImageView Home;
ListView listviewsms;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Msg = (ImageView) findViewById(R.id.createmessage);
Msg.setOnClickListener(this);
Home = (ImageView) findViewById(R.id.home);
Home.setOnClickListener(this);
listviewsms = (ListView) findViewById(R.id.lvSMS);
final List<String> msgList = getSMS();
adapter = new ArrayAdapter<String>(this, R.layout.conversation_list,R.id.name,msgList);
// updating listview
listviewsms.setAdapter(adapter);
}
public ArrayList<String> getSMS()
{
ArrayList<String> sms = new ArrayList<String>();
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSMSURI, String[] {"*"}, null, null, "date desc");
while (cursor.moveToNext())
{
String address = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
String read = cursor.getString(cursor.getColumnIndexOrThrow("read"));
//to fetch the contact name of the conversation
String contactName = address;
Uri Nameuri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cs= getContentResolver().query(Nameuri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
if(cs.getCount()>0)
{
cs.moveToFirst();
contactName = cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
sms.add(contactName + "\n"+body);
}
for (int i = 0; i < sms.size(); i++)
{
Log.e(" "," "+sms.get(i));
}
return sms;
}
public void onClick(View v)
{
//button functionality
}
}
How do I resolve this. Please Help. Thanks!
回答1:
For samsung phones you need to use this uri Uri.parse("content://mms-sms/conversations?simple=true");
And do not pass null
as the projection.
Add new String[] {"*"}
as the projection.
回答2:
Try this code,
public List<String> getSMS(){
List<String> sms = new ArrayList<String>();
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
while (cur.moveToNext()) {
String address = cur.getString(cur.getColumnIndex("address"));
String body = cur.getString(cur.getColumnIndexOrThrow("body"));
sms.add("Number: " + address + " .Message: " + body);
}
return sms;
}
try to set selection as date between date('now', '-1 day') and date('now'), remove selectionArgs
Update:
Cursor cur = getContentResolver().query(uriSMSURI, projection, "datetime(date/1000, 'unixepoch') between date('now', '-1 day') and date('now')", null, null);
Try this site,
http://www.itcuties.com/android/read-sms/
来源:https://stackoverflow.com/questions/18395988/retrieving-conversations-from-samsung-grand-android