i'm developing an android application which will filter out the messages from a particular number and read its message body.
I have written a class which extends to broadcast receiver as follows.
package com.tutecentral.restfulapiclient;
import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class Filter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[])extras.get("pdus");
if (pdus.length < 1) return;
StringBuilder sb = new StringBuilder();
String sender = null;
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (sender == null){
sender = message.getOriginatingAddress(); //get the sender
}
String text = message.getMessageBody(); //get the message body
Log.d("my broadcast","works");
//System.out.println("tsettexttt"+text);
if (text != null){
sb.append(text);
}
System.out.println("texttst"+text);
}
String number = "+94716355075"; //add the number
abortBroadcast();
}
}
}
}
Now what i want to do is to call this class from the main activity and also get the results from this class (String text : which holds the message body) so that i can use that value for other options in the app.
I went through every stackoverflow question posted regarding this but couldn't find the answer. Please be kind enough to help me out!!
Thanks in advance,
Regards
You can open your activity from the BroadcastReceiver passing some parameters, once the activity is open if certain parameters have been passed to this activity, call the method you want.
Hope it helps!
Edit: check this code out!
#Override
public void onReceive(Context context, Intent intent) {
//Do your normal work...
//start activity
Intent i = new Intent(context,YourActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("param","value"); //your parameter
context.startActivity(i);
}
Related
I think I'm in a bit of a catch 22 situation. I'm making an app that verifies the user's phone number by sending a text message to the number the user entered and then checking - that way it's definitely the user's phone number. This activity should only appear once, at the beginning - for the user to verify, and from then on activity 2 should be loaded.
I am trying to do this with :
// when the form loads, check to see if phoneNo is in there
SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
String phoneNoCheck = sharedPreferences.getString("phonenumber","");
if (phoneNoCheck != null) {
// if it is in there, start the new Activity
Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
MainActivity.this.startActivity(myIntent);
}
But it doesn't seem to be working properly. For a start, I'm not even sure it should be null. When I turn off my phone and on again, it starts back at activity 1, asking for verification.
And I can't test it. I need to run the app on my device as I can't send an SMS from the emulator, and I'm unable to get to the files where sharedPreferences exist on my device, so I can't even see if it's being written correctly.
Any advice on how I might be able to see the MyData.xml on my phone and proceed with testing my app, or tell my how I may be able to improve my code ? I downloaded the Astro app but still couldn't find it.
Here's my code :
package com.example.chris.tutorialspoint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.provider.Telephony;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.tutorialspoint.R;
public class MainActivity extends Activity {
Button sendBtn;
EditText txtphoneNo;
String phoneNo;
String origNumber;
private BroadcastReceiver receiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editText);
// when the form loads, check to see if phoneNo is in there
SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
String phoneNoCheck = sharedPreferences.getString("phonenumber","");
if (phoneNoCheck != null) {
// if it is in there, start the new Activity
Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
MainActivity.this.startActivity(myIntent);
}
//if it is not in there, proceed with phone number verification
else {
sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
IntentFilter filter = new IntentFilter();
// the thing we're looking out for is received SMSs
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
if (extras == null)
return;
Object[] pdus = (Object[]) extras.get("pdus");
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
origNumber = msg.getOriginatingAddress();
Toast.makeText(getApplicationContext(), "Originating number" + origNumber, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Sent to number" + phoneNo, Toast.LENGTH_LONG).show();
}
};
registerReceiver(receiver, filter);
}
}
protected void sendSMSMessage() {
phoneNo = txtphoneNo.getText().toString();
//this is the SMS received
String message = "Verification test code. Please ignore this message. Thank you.";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
//if originating phone number is the same as the sent to number, save
//and go to the next activity
if (origNumber.equals(phoneNo)) {
//save the phone number
SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("phonenumber", phoneNo);
editor.commit();
Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
MainActivity.this.startActivity(myIntent);
}
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS failed, please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
#Override
protected void onDestroy() {
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
super.onDestroy();
}
What do you expect to happen here:
String phoneNoCheck = sharedPreferences.getString("phonenumber","");
if (phoneNoCheck != null) {
...
}
The method that you are calling, getString(String, String), uses the second parameter as the default value, that is, if no property is found, it will default to, as you have defined, an empty string, "".
You then try and compare this to null, which will never be equal.
Try changing it up to something like this:
String phoneNoCheck = sharedPreferences.getString("phonenumber", null);
// check if null or empty
if ( null == phoneNoCheck || phoneNoCheck.equals("") ) {
// unregistered
}
package com.m.omg;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver
{
MediaPlayer mp =new MediaPlayer();
#Override
public void onReceive(Context context, Intent intent)
{
String mymsg = null;
String num = null;
//get the SMS message passed in
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null)
{
//retrieving the received SMS
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]);
num = msgs[i].getOriginatingAddress();
mymsg = msgs[i].getMessageBody().toString();
}
Toast.makeText(context, "from" + num + ":" + mymsg, Toast.LENGTH_LONG).show();
//making decision, depending on the number & message body
if (num.equalsIgnoreCase("some# :) ")){
abortBroadcast();
if (mymsg.equalsIgnoreCase("start")){
mp.create(context, R.raw.music);
mp.start();
}
}
mp.release();
}
}
}
*media player not initialized when the condition is matched... the app works normally, when I receive the SMS, the Toast is displayed but media player is not triggered, I get this warning in eclipse "The static method create(Context, int) from the type MediaPlayer should be accessed in a static way" which I ignored it :D *
use
mp=MediaPlayer.create(context, R.raw.music);
instead of
mp.create(context, R.raw.music);
for initializing mp instance of MediaPlayer
In my code I have a java file with this in it:
package com.myapp.basic;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiverActivity extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Parse the SMS.
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
// Retrieve the SMS.
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]);
// In case of a particular App / Service.
//if(msgs[i].getOriginatingAddress().equals("+91XXX"))
//{
//str += "SMS from " + msgs[i].getOriginatingAddress();
//str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
//}
}
if (str != "") { // remove the last \n
str = str.substring(0, str.length()-1);
}
// Need to find a better way to check if the activity is running
try {
((Police_ViewActivity) context.getApplicationContext()).handle_incoming_help_message(str); // crash here
} catch(Exception e) {
System.out.println(e.getMessage());
}
System.out.println("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
}
}
}
When the code in the try block happens, it crashes and i get this error:
08-27 22:45:11.345: I/System.out(27606): android.app.Application cannot be cast to com.escortme.basic.Police_ViewActivity
How can I fix this ?
Calling Activity is best using Intent. You can just put something like:
Intent intent = new Intent(Police_ViewActivity.class);
intent.putExtra("help_message", str);
context.startActivity(intent);
In onResume() of your Police view Activity, grab the extra and handles it:
onResume(){
String msg = getIntent().getStringExtra("help_message");
if (msg==null) return;
handleMessage(msg);
}
You don't have to worry multiple instance of the Activity being created, in manifest you can specify how the activity launch using Launchmode. singleInstance could be your choice.
If you just need to call handle_incoming_help_message method in another class, just make handle_incoming_help_message method static.
update:
So you should start Police_ViewActivity and tell it that you need to call handle_incoming_help_message
Intent intent = new Intent(context, Police_ViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra("handle_incoming_help_message", str);
context.startActivity(intent);
and in Police_ViewActivity's onCreate (or onResume or Onstart)
String str = getIntent().getStringExtra("handle_incoming_help_message");
if(str != null)
handle_incoming_help_message(str);
Update 2
To keep the number of instances of policeviewactivity to be at most 1? -> Android LaunchMode
In my application I want to receive SMS and afert that, I want to get the the location of the phone and when I have the location, I want to send it By SMS...
by now, there is what I have :
package com.receiver;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver{
private final String ACTION_RECEIVE_SMS = "android.provider.Telephony.SMS_RECEIVED";
private String numero;
private String msg;
#Override
public void onReceive(Context context, Intent intent){
Log.i("ReceiveBootCompleted","****** Boot terminer ********************");
Log.i("ReceiveBootCompleted"," ***** lancement du service Test **************");
context.startService(new Intent().setComponent(new ComponentName(context.getPackageName(), SMSReceiver.class.getName())));
if (intent.getAction().equals(ACTION_RECEIVE_SMS)){
Bundle bundle = intent.getExtras();
if (bundle != null){
Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++){
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
if (messages.length > -1){
final String messageBody = messages[0].getMessageBody();
final String phoneNumber = >messages[0].getDisplayOriginatingAddress();
if(messageBody.equals("mollo")){
app.smartfinder.SmartFinderActivity.send_message(phoneNumber);
}
}
}
}
}
and :
package app.smartfinder;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
public class SmartFinderActivity extends Activity implements LocationListener {
LocationManager lm;
private Location location;
private double lat = 0;
private double lng = 0;
TextView position;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, this);
position = (TextView)findViewById(R.id.var_current_pos);
}
public void onClick_conf(View view){
Toast.makeText(SmartFinderActivity.this, "config", Toast.LENGTH_LONG).show();
Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
startActivity(settingsActivity);
}
public void onClick_hist (View view){
Toast.makeText(SmartFinderActivity.this, "history", Toast.LENGTH_LONG).show();
}
public void onLocationChanged(Location location) {
lat = location.getLatitude();
lng = location.getLongitude();
afficherAdresse();
position.setOnClickListener(new OnClickListener() { public void onClick(View v) {
Intent intent_map = new Intent(getBaseContext(), Map.class);
intent_map.putExtra("lat", lat);
intent_map.putExtra("lng", lng);
startActivity(intent_map);
}
});
lm.removeUpdates(this);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void afficherAdresse() {
Geocoder geo = new Geocoder(SmartFinderActivity.this);
try {
List<Address> adresses = geo.getFromLocation(lat,lng,1);
if(adresses != null && adresses.size() == 1){
Address adresse = adresses.get(0);
//if geocoder find a adresse, we can use it
position.setText( adresse.getAddressLine(0) + " " + adresse.getPostalCode() + " " + adresse.getLocality());
}
else {
//else: no adress
position.setText("L'adresse n'a pu être déterminée");
}
} catch (IOException e) {
e.printStackTrace();
position.setText("L'adresse n'a pu être déterminée");
}
}
public static void send_message(String phoneNumber) {
// `refresh Location : that's the problem ! `
// send method : not difficult
}
}
My problem is that I want to refresh my position when method "send_message" is called.
thank you for your help
I think that you're asking how to get your current location when you receive an SMS message so that you can send back the current location in another SMS message. I think that you are assuming that your are able to just make a call to get your current location to report back.
GPS doesn't work that way. It needs to run for a little while to get enough messages from enough satellites to be able to determine a position and an accuracy for that position. So there is no single function call to simply get the device location instantly.
Instead it is necessary to run a background task (I use an Async Task) to listen to position updates and the iterate to the current location. My task turns off the listening (and the GPS hardware) when it has a good enough fix to save battery life.
See a previous related answer for more information.
It is possible to ask the Location Manager for the current position. However, this is the last position that it recorded. If no application has used the GPS hardware for a while then that postion fix could be very old and therefore not accurate at all. At work, my building shields the GPS signals (metal cladding) and so my phone currently believes that it is at my home many miles away which is where I last used a GPS enabled app.
As SMS takes a variable time to be delivered I think that it is probably correct to take a few extra seconds to fire off the GPS collection and wait for a good enough fix before responding. SMS is not an instant delivery vehicle so the extra few seconds delay in getting a good fix will not add much to the round-trip time of your SMS messages.
Send Text Messages (SMS) From Android When Near A Predefined Location...
Ex. i enter in the college or out from the college that time my Android device check my current position if it match with predefine position then my device send automatic sms to other no.
Any buddy have idea or code related to this ..
thank you..
I put whole code from my SMS util. You should have a look at sendSms function. The util allows you to watch for incoming sms and track sms sent by you ( If you want to do that ).
The next part is to handle location updates. The best way how to do it depends on many things. You can obtain location via LocatinProviders ( GPS, wireless or passive ) or read cell info from TelephonyManager. Below you have some details about them:
LocationProvider:
returns geo lat/lon data
you can not read data if user disabled "Use GPS satellites" and "User wireless networks"
you will rather not get data if you are in a building ( no GPS signal there ).
you have to wait very long for the location.
very good accuracy.
can drain battery a lot.
"pasive" provider can be a good choice for you.
Cells.
returns the neighboring cell information of the device.
location is not available if your device is not connected to gsm/cdma network ( no sim card ).
not good accuracy but rather for you purpose will be ok.
doesn't kill battery so much.
Which option is better for you ?
package android.commons;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
public final class SmsModem extends BroadcastReceiver {
private static final String SMS_DELIVER_REPORT_ACTION = "android.commons.SMS_DELIVER_REPORT";
private static final String SMS_DELIVER_REPORT_TOKEN_EXTRA = "token";
private static final String TAG = SmsModem.class.getSimpleName();
private final Context context;
private final SmsManager smsManager;
private final SmsModemListener listener;
private final Map<String, Integer> pendingSMS = new HashMap<String, Integer>();
public interface SmsModemListener {
public void onSMSSent(String token);
public void onSMSSendError(String token, String errorDetails);
public void onNewSMS(String address, String message);
}
public SmsModem(Context c, SmsModemListener l) {
context = c;
listener = l;
smsManager = SmsManager.getDefault();
final IntentFilter filter = new IntentFilter();
filter.addAction(SMS_DELIVER_REPORT_ACTION);
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
context.registerReceiver(this, filter);
}
public void sendSms(String address, String message, String token) {
if ( message != null && address != null && token != null) {
final ArrayList<String> parts = smsManager.divideMessage(message);
final Intent intent = new Intent(SMS_DELIVER_REPORT_ACTION);
intent.putExtra(SMS_DELIVER_REPORT_TOKEN_EXTRA, token);
final PendingIntent sentIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
final ArrayList<PendingIntent> intents = new ArrayList<PendingIntent>();
for ( int i = 0 ; i < parts.size() ; i++ ) {
intents.add(sentIntent);
}
pendingSMS.put(token, parts.size());
smsManager.sendMultipartTextMessage(address, null, parts, intents, null);
}
}
public void clear() {
context.unregisterReceiver(this);
}
#Override
public void onReceive(Context c, Intent intent) {
final String action = intent.getAction();
if ( action.equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
final Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdusObj.length];
for (int i = 0; i<pdusObj.length; i++) {
messages[i] = SmsMessage.createFromPdu ((byte[]) pdusObj[i]);
final String address = messages[i].getDisplayOriginatingAddress();
final String message = messages[i].getDisplayMessageBody();
listener.onNewSMS(address, message);
}
}
} else if ( action.equalsIgnoreCase(SMS_DELIVER_REPORT_ACTION)) {
final int resultCode = getResultCode();
final String token = intent.getStringExtra(SMS_DELIVER_REPORT_TOKEN_EXTRA);
Log.d(TAG, "Deliver report, result code '" + resultCode + "', token '" + token + "'");
if ( resultCode == Activity.RESULT_OK ) {
if ( pendingSMS.containsKey(token) ) {
pendingSMS.put(token, pendingSMS.get(token).intValue() - 1);
if ( pendingSMS.get(token).intValue() == 0 ) {
pendingSMS.remove(token);
listener.onSMSSent(token);
}
}
} else {
if ( pendingSMS.containsKey(token) ) {
pendingSMS.remove(token);
listener.onSMSSendError(token, extractError(resultCode, intent));
}
}
}
}
private String extractError(int resultCode, Intent i) {
switch ( resultCode ) {
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
if ( i.hasExtra("errorCode") ) {
return i.getStringExtra("errorCode");
} else {
return "Unknown error. No 'errorCode' field.";
}
case SmsManager.RESULT_ERROR_NO_SERVICE:
return "No service";
case SmsManager.RESULT_ERROR_RADIO_OFF:
return "Radio off";
case SmsManager.RESULT_ERROR_NULL_PDU:
return "PDU null";
default:
return "really unknown error";
}
}
}