Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:42:14+00:00 2026-06-05T18:42:14+00:00

Hi I’m making an app which uses only the broadcast receiver. It serves three

  • 0

Hi I’m making an app which uses only the broadcast receiver. It serves three purposes. When a SMS containing specific code is received it sends the GPS data. The other code sends the sensor data. No problems up to this point. But for the battery I’m unable to do this because of the registerreceiver() function. As I’m not using an activity and onCreate method, can I send the battery data when an SMS is received?

When I tried to tweak this in the onreceive() method of broadcastreceiver, my application crashes. I searched everywhere an activity is created. I want to do everything inside the broadcast receiver only.

My code goes like this:

public class MyPhoneReceiver extends BroadcastReceiver{
double current_lat,current_lng;
LocationManager mlocManager;
LocationListener mlocListener;
//addition//////////////////////////////
SensorManager mSensorManager;

Sensor mAccelerometer;  
////////////////////

private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "SMSBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Intent recieved: rama" + intent.getAction());


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) {
String messagebody=messages[0].getMessageBody();
Log.i(TAG, "Message recieved: in message receied " + messages[0].getMessageBody());
if(messagebody.toString().matches("HDPK GPS"))
{
Log.i(TAG, "Message recieved: HDPK RECEIVED " + messages[0].getMessageBody());
mlocManager=(LocationManager)context.getSystemService(context.LOCATION_SERVICE);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,1,new MyLocationListener());
}
if(messagebody.toString().matches("HDPK SENS"))
{
Log.i(TAG, "Message recieved: in msensior ");
mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(new MySensorListener(), mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
if(messagebody.toString().matches("HDPK BAT"))
{
Log.i(TAG, "Message recieved: in battery ");
IntentFilter filter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batt=context.registerReceiver(null,filter);
int level = batt.getIntExtra("EXTRA_LEVEL", 0);
Log.i(TAG, "Message recieved: in battery level is "+level);
SmsManager sender=SmsManager.getDefault();
sender.sendTextMessage("9762281814", null, "Level is"+level, null,null);
}



}
}
}

public class MySensorListener implements SensorEventListener
{
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub

}

public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
Log.i(TAG, "Message recieved: in msensior event change ");
float x = event.values[0];

float y = event.values[1];

float z = event.values[2];
SmsManager sender=SmsManager.getDefault();
String Text="x :"+x+" Y:"+y+ " Z:"+z;
sender.sendTextMessage("9762281814",null, Text, null, null);
mSensorManager.unregisterListener(this);
}

}

public class MyLocationListener implements LocationListener{

public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
current_lat=location.getLatitude();
current_lng=location.getLongitude();
SmsManager sender=SmsManager.getDefault();
String Text = "My Location is : Latitude ->" +current_lat+" Longitude ->" + current_lng;
sender.sendTextMessage("9762281814", null, Text, null, null);

mlocManager.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

}


}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:42:16+00:00Added an answer on June 5, 2026 at 6:42 pm

    Try using the application Context instead of the broadcast receiver Context when using registerReceiver() to get the sticky battery changed Intent. Like this:

    IntentFilter filter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batt=context.getApplicationContext().registerReceiver(null,filter);
    int level = 99; // Default to some unknown/wild value
    // registerReceiver method call could return null, so check that!
    if (batt != null) {
        level = batt.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.