application not going to intent part - android

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);

Related

SMS receiving code works on one device but not on other device (Micromax E4820)

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);
}

received sms from special number go to my program and other received sms go to inbox of phone

I wrote send and receive program in android. when sms received from special number, sms go to my program and body of sms , show in text. but for received sms from other phone number, sms go to inbox of phone and program isn't opened.
Now in my program , for every received sms, program is opened and body of sms is shown in textview.
if condition didn't work!!!
i put my code, please check my code.
SmsReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class SmsReceiver extends BroadcastReceiver {
public String str = "";
static final String ACTION =
"android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null) {
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 msg_from = msgs[i].getOriginatingAddress();
if(msg_from.equals("+9891--------"))
{
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
Intent act = new Intent(context, MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra("message", str);
context.startActivity(act);
}
abortBroadcast();
}
}
}
You should do the context.startActivity(act); in the if condition. I think you mistakenly put it out of the if condition.
Also the abortBroadcast() should be in if condition coz that makes sense, if the msg is not from the special number it should go to the inbox....RIGHT :)
Edited:
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null) {
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 msg_from = msgs[i].getOriginatingAddress();
if (msg_from.equals("+9891--------")) {
String str = "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
Intent act = new Intent(context, MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra("message", str);
context.startActivity(act);
abortBroadcast();
}
}
}
}
}

How to read sms response line by line

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*/ }
}
}

how to pass a 2 dimention array as an intent extra in android

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);

String operation in array

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()]);

Categories

Resources