java - 在 Android 应用程序中接收彩信

标签 java android xml listview mms

我要开发一个 android 应用程序,它的 Activity 之一是:

  1. 检测新的彩信
  2. 获取发件人号码以检查它是否是我希望在我的应用程序中收到的号码
  3. 从彩信中获取 (txt + image)
  4. 在 ListView 中显示(Txt + 图像)数据

好吧,我找到了一个代码,可以帮助我完成第二个和第三个任务 但我尝试了它并在我的手机上对其进行了测试,它可以正常工作,但它没有检索到任何东西,只是黑色界面!!!

也许是我不理解这段代码的某些部分的问题,

我在每条我无法理解的陈述上方写下了我的评论,请在代码中回答我的评论并帮助我理解我所缺少的 希望早日得到您的答复。请说清楚。

   import java.io.BufferedReader;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.InputStreamReader;
   import java.text.MessageFormat;

   import android.app.Activity;
   import android.content.ContentResolver;
   import android.content.ContentValues;
   import android.content.Context;
   import android.database.Cursor;
   import android.graphics.Bitmap;
   import android.graphics.BitmapFactory;
   import android.net.Uri;
   import android.os.Bundle;
   import android.telephony.SmsMessage;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.ImageView;
   import android.widget.ListView;
   import android.widget.TextView;
     import android.app.Activity;
     import android.os.Bundle;

   public class MMSReceiverActivity extends Activity {

  private final Uri CONTENT_URI_PART = Uri.parse("content://mms/part");
   private static final String MSG_ID_STR = "mid=%1$s"; // I don't understand it

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Start Copying Code
    // I don't know how can I retrieve mms id
    long msg_id = this.getIntent().getLongExtra("msg_id", 0);
    // What's different between ViewGroup and ListView
    ViewGroup listview = (ViewGroup) findViewById(R.id.mmsdetaillist);
    // What's different between "content://mms/part" and "content://mms/"
    Cursor cursor = getContentResolver().query(CONTENT_URI_PART, null,
            String.format(MSG_ID_STR, msg_id), null, null);

    if (cursor.moveToFirst()) {
        do {
            // Why he puts partID
            String partId = cursor.getString(cursor.getColumnIndex("_id"));
            String type = cursor.getString(cursor.getColumnIndex("ct"));

            if ("text/plain".equals(type)) {
                String data = cursor.getString(cursor
                        .getColumnIndex("_data"));
                String body;
                // What's the different between if it's null or not all of
                // them will return (text)
                if (data != null) {
                    // implementation of this method below
                    body = getMmsText(partId);
                } else {
                    body = cursor.getString(cursor.getColumnIndex("text"));
                }

                // Why he declared it like this i mean why it didn't declare
                // like this findViewById (R.) etc
                TextView t = new TextView(this);
                t.setText(body);
                listview.addView(t);

                // Why here else ?? it should be only if because if MMS
                // contains Text + img : so it'll ignore the (else = img)
                // part !

            } else if ("image/jpeg".equals(type)
                    || "image/bmp".equals(type) || "image/gif".equals(type)
                    || "image/jpg".equals(type) || "image/png".equals(type)) {
                Bitmap bitmap = getMmsImage(partId);
                ImageView iv = new ImageView(this);
                iv.setImageBitmap(bitmap);
                listview.addView(iv);
            }
        } while (cursor.moveToNext());
    }
    cursor.close();

}

public String getMmsText(String id) {
    Uri partURI = Uri.parse("content://mms/part/" + id);
    InputStream is = null;
    StringBuilder sb = new StringBuilder();
    try {
        is = 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();
}

public Bitmap getMmsImage(String _id) {
    Uri partURI = Uri.parse("content://mms/part/" + _id);
    InputStream is = null;
    Bitmap bitmap = null;
    try {
        is = getContentResolver().openInputStream(partURI);
        bitmap = BitmapFactory.decodeStream(is);
    } catch (IOException e) {
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
    return bitmap;
}
 }

注意:每次我搜索发送或接收彩信时我都会得到这个链接,但直到现在我还没有得到它

我应该使用 repo 和 Git 吗? 什么是 repo 和 Git? 我为什么要在 MMS 中使用?

我应该在发送和接收彩信时使用它吗?如果是,为什么? 我可以使用这段代码吗

 content://mms-sms/conversations

而不是 repo 和 git?

repo and Git

最佳答案

您的大部分问题都与作业中的 MMS 部分无关。其中大部分是标准的 Java 和/或 Android,您在执行此类任务之前需要了解这些内容。

除此之外,不直接支持 SMS 和 MMS,而且大多是无文档的,因此很难使用。

我一直在研究短信和彩信,发现这些链接非常有用:

How to Read MMS Data in Android?

http://groups.google.com/group/android-developers/browse_thread/thread/d0c15ec17c12af0e?fwc=1&pli=1

既然我已经开始了,我不妨回答一些您提出的更简单的问题:

“ViewGroup 和 ListView”

ViewGroup 是大多数 View 的父类(super class),可以包含其他 View (LinearLayout、RelativeLayout ...),其中 ListView 是“创建可滚动项目列表的 ViewGorup”。

“他为什么这样声明我的意思是为什么它没有这样声明 findViewById (R.) 等”

当您有一个 xml 布局文件并想在该布局中引用一个 View 时,使用 findViewById。在这种情况下,他在运行时创建了一个 TextView(而不是在 xml 文件中定义它)并将其添加到 ListView。

关于java - 在 Android 应用程序中接收彩信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10087972/

相关文章:

c# - 带有 sqlXML 列的大型数据表抛出 System.OutOfMemoryException

java - 将 "Map<UUID, String>"接口(interface)替换为更简单的 "IMap"

Java - .class 文件中的 package 语句是否被删除?

android - socket.io - 无法在 Android 客户端中接收 "next(new Error())"事件?

java - 无法构建同时包含 Azure SDK 和 Google Play 的应用程序

Java - 确定xml文档的大小

java - 如何实现轮询循环链表并统计元素的访问请求?

java - java迭代器似乎正在丢失数据

android - CheckBox onCheckedListener 不工作数据绑定(bind)

java - 将 XML 解析为 Java 列表