i've been trying to develop an application that displays all your text messages in the old fashioned nokia style, which means you have a listview with the contact that sent you a message, and when you click on that, your message appears in a textview. So, here is what i did :- I made a messageoutline class
package com.one.inbox;
public class MessageOutline {
private String phone;
private String message;
public MessageOutline(String phone, String message){
setPhone(phone);
setMessage(message);
}
public void setPhone(String Phone){
MessageOutline.this.phone=phone;
}
public void setMessage(String Message){
MessageOutline.this.message=Message;
}
public String getPhone(){
return this.phone;
}
public String getMessage(){
return this.message;
}
}
In the mainactivity i have this arraylist which i use in arrayadater and call setlistadapter by using the arrayadapter object instantiated with the arraylist :-
package com.one.inbox;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
public static ArrayList<MessageOutline> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList=new ArrayList<MessageOutline>();
ArrayAdapter<MessageOutline> array=new ArrayAdapter<MessageOutline>(this, android.R.layout.simple_list_item_1, mList);
setListAdapter(array);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
then there is this messagelistener which is a broadcast receiver, in which i add objects to the arraylist
package com.one.inbox;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class MessageListener extends BroadcastReceiver{
final SmsManager sms=SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
MainActivity.mList.add(new MessageOutline(senderNum,message));
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}
here is my mainactivity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
and here goes the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.one.inbox"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.one.inbox.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.one.inbox.MessageListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
i dont know whats going wrong as the logcat is showing no exceptions, and the activity is not stopping, but nothing is happening in the main screen.
What i want is the mainscree should show a list with contact name or number, and when you click on them, a textview with the message body must open, i havent implemented the onlistitemclick listener yet, coz, i want to solve the first problem as nothing is showing in the screen.
any help would be most humbly appreciated. Thanx.