1

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

}


}

}
4

1 回答 1

7

尝试使用应用程序Context而不是广播接收器ContextregisterReceiver()更换粘性电池Intent。像这样:

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);
}
于 2012-06-08T08:36:35.653 回答