how to resolve this error The operator += is undefined for the argument type(s) String[][], String
SmsMessage[] msgs = null;
String [][]str = null;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
The error pretty much says it all - you can't use += with an array in Java. Why not use a List to build up the String values and then convert it into an array when you are finished?
List<String> str = new ArrayList<String>();
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str.add("SMS from " + msgs[i].getOriginatingAddress());
str.add(" :");
...
String[] strArray = str.toArray(new String[str.size()]);
Related
onReceive method doesn't gets called.
I checked the broadcast receiver code for calls and it works fine.
The following code works on my Intex Elyt Dual (7.0) but don't work on other with 6.0.1.
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "on Receive", Toast.LENGTH_SHORT).show();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; ++i) {
// Convert Object array
hereComesNewSMS = hereComesNewSMS++ ;
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
// Sender's phone number
str += "SMS from " + msgs[i].getOriginatingAddress() + " : ";
// Fetch the text message
str += msgs[i].getMessageBody().toString();
str += "\n";
latestSMSnumber = msgs[i].getOriginatingAddress();
latestSMScontent = str;
}
// Display the entire SMS Message
Log.e("TAG1 number: ", latestSMSnumber);
Log.e("TAG2 content: ", str);
}
I am developing an android app which gets the SMS using broadcast receiver when its comes. Here is the code:
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs;
if (bundle != null) {
//---retrieve the SMS message received---
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
String address = msgs[i].getOriginatingAddress();
String body = msgs[i].getMessageBody();
}
} catch(Exception e) {
Log.d("Exception caught",e.getMessage());
}
}
}
Problem is that few messages I receive comes in two or three parts. I don't know how to join these parts of messages. How can I detect that the first part of message needed to be combined with the next part message.
First Part: Your airtel mobile ********** online recharge txn ID ************ o
Second Part: f Rs *** has been initiated. Please keep the txn id for future refe
Third Part: rence.
You need to add message body(parts).
change this
String body = msgs[i].getMessageBody();
to
String body += msgs[i].getMessageBody();
The problem is inside your loop try this out, you should be fine
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
// Retrieve the SMS Messages received
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
// For every SMS message received
for (int i=0; i < msgs.length; i++) {
// Convert Object array
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
// Sender's phone number
str += "SMS from " + msgs[i].getOriginatingAddress() + " : ";
// Fetch the text message
str += msgs[i].getMessageBody().toString();
str += "\n";
}
// Display the entire SMS Message
Log.d(TAG, str);
}
This is my code to read sms. I want to read received sms line by line if line one contain no 00 do this if line 2 contain this String do this etc.
Below is my code which only read smss containing 00 then do this not read line by line how I change this code to read sms line by line
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString().split("/n");;
str += "\n";
if(str.contains("00"));
{
Intent l = new Intent(context,AgAppMenu.class);
l.putExtra("msg",str);
l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(l);
}
Use String.split().
For example to get the different lines from a String you do:
String example = "Hello\nWorld!";
String[] parts = example.split("\n");
// parts[0] contains "Hello"
// parts[1] contains "World!"
For your case, to get each line of the message, use something like :
String[] msg_lines = msgs[i].getMessageBody().toString().split("/n");
So your code may look like:
String[] msg_lines = null;
for(...) {
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
msg_lines = msgs[i].getMessageBody().toString().split("\n");
for (String line : msg_lines)
{
if (line.contains("00")) { /*then*/ }
}
}
this is my application code below which nto going to intent part i used debug i find is not going after line
xmlResponse2[0][i]=test[i];
wht i mistake?? can anyone help me is not launch any activity and code broke after
xmlResponse2[0][i]=test[i]; this line
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
String[][] xmlResponse2= null;
String[] test = str.split("\n");
xmlResponse2= new String[0][test.length];
for (int i = 0; i < test.length; i++) {
xmlResponse2[0][i]=test[i];
Intent l = new Intent(context,AgAppMenu.class);
l.putExtra("msg",xmlResponse2);
l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(l);
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
This statement:
xmlResponse2= new String[0][test.length];
creates an array with 0 elements (ie: with NO elements). Maybe you want to create an array with 1 element, like this:
xmlResponse2= new String[1][test.length];
xmlResponse2= new String[test.length];
for (int i = 0; i < test.length; i++) {
xmlResponse2[i]=test[i];
}
Intent l = new Intent(context,AgAppMenu.class);
l.putExtra("msg",xmlResponse2);
// l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(l);
I pass 2d array [][] object as an intent extra to another activity, but its show error i can not be identifies what do i do?
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
String[][] xmlResponse2= null;
String[] test = str.split("\n");
xmlResponse2= new String[0][test.length];
for (int i = 0; i < test.length; i++) {
xmlResponse2[0][i]=test[i];
}
Intent l = new Intent(context,AgAppMenu.class);
l.putExtra("msg",xmlResponse2[0][i]);
l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(l);
Intent.putExtra does not accept 2 dimension array as per the following document
http://developer.android.com/reference/android/content/Intent.html
I have check the source code of Intent.putExtra , It use Bundle Class to store data
http://developer.android.com/reference/android/os/Bundle.html
Here is Source code of Intent.java
Intent.java http://gitorious.org/android-eeepc/base/blobs/fda6fae156e31a287e3cfbf66e51ea1405cdf479/core/java/android/content/Intent.java
Bundle.java https://github.com/android/platform_frameworks_base/blob/master/core/java/android/os/Bundle.java
Bundle.java use "HashMap" to store the content,
Best of my knowledge it's not possible directly
you can use putParcelable method, you need to create your own class instanceof 2 dimension array.
one quick solution
String[] keys = createKeys();
String[] values = createValues();
(keys.length == values.length)
bundle.putExtra("keys", keys);
bundle.putExtra("values", values);