第二个最近收到的彩信,而不是Android中的最新彩信

时间:2013-06-10 21:37:11

标签: android cursor mms android-contentresolver

最近,我一直在研究一个Android项目,要求我从中获取文本和MMS。我没有很多Android经验,但我熟悉SQL和java,我一直在尽可能地关注这些帖子。

我提出的代码主要是有效的,但似乎总是从第二个最近的MMS消息中获取文本,而不是最新的。知道为什么会这样吗?

以下是我使用前面提到的两个主题作为指南制作的MMSReceiver类的相关部分。

首先,是onReceive方法......    公共类MMSReceiver扩展了BroadcastReceiver {

private static final String ACTION_MMS_RECEIVED = "android.provider.Telephony.WAP_PUSH_RECEIVED";
private static final String MMS_DATA_TYPE = "application/vnd.wap.mms-message";
private static String message;

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("Test" , "MMSReceiver!");
    Uri uri = Uri.parse("content://mms-sms/conversations");     
    String action = intent.getAction();
    String type = intent.getType();
    //Log.d("MMS", "action is " + action + " , type is " + type);
    final Context cont = context;
    ContentResolver cr = context.getContentResolver();
    final String[] projection = new String[]{"*"};
    Uri MMSuri = Uri.parse("content://mms/inbox/");
    Cursor MmsIDquery = cr.query(uri, projection, null, null, null);
    MmsIDquery.moveToFirst();
    String mid = MmsIDquery.getString(MmsIDquery.getColumnIndex("_id"));
    Log.d("MMS", "message id for unreadable message is " + mid);
    message = getMessageText(cont, mid);
    ...

getMessageText方法传递上下文和消息id,并写成这个......

    /* gets message text */
public String getMessageText(Context context, String mmsid){
    Log.d("MMS", "mmsid was " + mmsid);
    String message = null;
    String mid = ""+(Integer.parseInt(mmsid)+2);
    Log.d("MMS", "mmsid is now " + mid);
    String selectionPart = "mid=" + mmsid;
    Uri mmsTextUri = Uri.parse("content://mms/part");
    Cursor cursor = context.getContentResolver().query(mmsTextUri, null, selectionPart, null, null);
    if (cursor.moveToFirst()){
        do{
            String partId = cursor.getString(cursor.getColumnIndex("_id"));
            String type = cursor.getString(cursor.getColumnIndex("ct"));
            Log.d("MMS", "getMessageText was called, partId = " + partId + " , type = " + type);
            if ("text/plain".equals(type)){
                String data = cursor.getString(cursor.getColumnIndex("_data"));
                Log.d("MMS", "data was " + data);
                if (data != null){
                    message = getMmsText(context, partId);
                    Log.d("MMS", "body was " + message);
                } else {
                    message = cursor.getString(cursor.getColumnIndex("text"));
                    Log.d("MMS", "body was " + message);
                }
            }
        } while (cursor.moveToNext());
    } else {
        Log.d("MMS", "Query returned nothing in getMessageText()");
    }
    return message;
}

在此方法中,将调用getMmsText。我意识到这个过程有点多余,但是我很难理解在这个过程中发生的所有事情所以我保持它与原始线程类似,以确保问题不是以我为自己的代码减少它的方式。

getMmsText看起来像这样......

    public String getMmsText(Context c, String id){
    Log.d("MMS", "getMmsText was called with " + id);
    Uri partUri = Uri.parse("content://mms/inbox/" + id);
    InputStream is = null;
    StringBuilder sb = new StringBuilder();
    try{
        is = c.getContentResolver().openInputStream(partUri);
        if (is != null){
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            BufferedReader reader = new BufferedReader(isr);
            String temp = reader.readLine();
            while(temp != null){
                sb.append(temp);
                temp = reader.readLine();
            }
        }
    } catch (IOException e){}
    finally {
        if (is != null){
            try{
                is.close();
            } catch (IOException e){}
        }
    }
    return sb.toString();
}

非常感谢您的帮助,如果您有任何问题可以帮助您解答,请与我们联系。

1 个答案:

答案 0 :(得分:1)

我,我自己并没有完全完成整个过程,但我正在研究它,这是我迄今为止所学到的。我记得在听之前读取android.provider.Telephony.WAP_PUSH_RECEIVED只会通知你收到的彩信。我的理解是,意图更多的是收据,并不意味着MMS已经在手机中,或者更准确地说,在数据库中来自。这可以解释为什么你只是倒数第二。如果您想在MMS完成并准备好从数据库中取出MMS,我认为您需要ContentObserver来通知您数据库更新。