Receiving MMS in android application
I need to develop an android app and one of its actions:
- Detecting new MMS messages
- Get the sender number to check if this is the number I want to get from him in my application.
- Receiving (txt + image) from MMS
- Displaying (Txt + Image) Data in ListView
well i found some code that could help me in 2nd and 3rd tasks but i tried this and tested it on my mobile and it works but it didn't get anything but black interface!
Perhaps the problem is that I did not understand some part of this code,
I wrote my comment above each statement that I could not understand it, please respond to my comments what is inside the code and help me understand what I am not seeing Hopefully the answers are early. and clear up.
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 different between ViewGroup and ListView
ViewGroup listview = (ViewGroup) findViewById(R.id.mmsdetaillist);
// What 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 the different between if it 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;
}
}
Note: everyTime I search when sending or receiving MMS, I got this link but still I don't understand it
Should I be using repo and Git? and what is repo and git? why should i use MMS?
Should I use it when sending and receiving MMS? if yes for what? can i use this code
content://mms-sms/conversations
instead of repo and git?
source to share
Most of your questions have nothing to do with the MMS part of your assignment. Most of them are standard java and / or Android, which you need to understand before accepting such a task.
Also, SMS and MMS are not directly supported and are mostly undocumented, making them very difficult to work with.
I played around with sms and mms and found these links very helpful:
How to read MMS data on Android?
http://groups.google.com/group/android-developers/browse_thread/thread/d0c15ec17c12af0e?fwc=1&pli=1
And now that I am, I could answer some of the simpler questions you have:
"ViewGroup and ListView"
ViewGroup is the superclass for most views that can contain other views (LinearLayout, RelativeLayout ...), where the ListView is a "ViewGorup that creates a list of scrollable items".
"Why did he declare it like that, I mean why didn't he declare it findViewById (R.), etc."
findViewById is used when you have an xml layout file and want to reference a view in that layout. In this case, it creates the TextView at runtime (instead of defining it in the XML file) and adds it to the ListView.
source to share