I am making an app, its for polling through SMS. What I want is if someone says “yes” I get a +1 on my yes count and similarly for the “no” case, and I want to this to be done in background, so I am using service. and show the end result on my MainActivity. I tried, and made the following code::
BroadcastReciver
/*Bundle bundle = intent.getExtras();
SmsMessage[] msg = null;
String body = "";
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msg = new SmsMessage[pdus.length];
for (int i = 0; i < msg.length; i++) {
body = msg[i].getMessageBody().toString();
}
}*/
String body = "yes";
if(body.equals("YES") || body.equals("yes")|| body.equals("y") || body.equals("Y")){
Intent yes = new Intent(context,PollCounter.class);
yes.putExtra("yes", true);
yes.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(yes);
context.stopService(yes);
}
else if(body.equals("NO") || body.equals("no")|| body.equals("n") || body.equals("N")){
Intent no = new Intent(context,PollCounter.class);
no.putExtra("no", true);
no.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(no);
context.stopService(no);
}
else {
Intent invalidMsg = new Intent(context,PollCounter.class);
invalidMsg.putExtra("invalidMsg", true);
context.startService(invalidMsg);
}
}
}
There is a problem in the BroadcastReceiver also, I get a ” E/AndroidRuntime(708): java.lang.RuntimeException: Unable to start receiver com.atiffarrukh.smspolling.SMSBroadcastReciever: java.lang.NullPointerException” Error, when i uncomment the following
/*Bundle bundle = intent.getExtras();
SmsMessage[] msg = null;
String body = "";
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msg = new SmsMessage[pdus.length];
for (int i = 0; i < msg.length; i++) {
body = msg[i].getMessageBody().toString();
}
}*/
Dont get the reason, I used this code before and it was working.
In the Service ::
boolean yes = intent.getBooleanExtra("yes", false);
boolean no = intent.getBooleanExtra("no", false);
boolean invalidMsg = intent.getBooleanExtra("invalidMsg", false);
if (yes) {
yesCount++;
Intent intentYes = new Intent(this,MainActivity.class);
intentYes.putExtra("yesCount", yesCount);
startActivity(intentYes);
Toast.makeText(getBaseContext(), "yes is recvd", Toast.LENGTH_LONG).show();
} else if (no) {
noCount++;
Intent intentNo = new Intent(this,MainActivity.class);
intentNo.putExtra("yesCount", noCount);
startActivity(intentNo);
Toast.makeText(getBaseContext(), "no is recvd", Toast.LENGTH_LONG).show();
} else {
invalidMsgCount++;
Intent intentInavalidMsg = new Intent(this,MainActivity.class);
intentInavalidMsg.putExtra("yesCount", invalidMsgCount);
Toast.makeText(getBaseContext(), "Invalid is recvd", Toast.LENGTH_LONG).show();
}
}
I tried to use Bundle b = intent.getExtras(); but I am not sure how to use it, and what should I initialize intent with.
Thanks…
P.S: I am new to android. Sorry if my question is a bit weird or my coding seems strange.
it seems you forgot
in your cycle.
It should be
otherwise
getMessageBody()indeed returnsnull