嗨,我正在制作一个仅使用广播接收器的应用。它有三个目的。当收到包含特定代码的SMS时,它发送GPS数据。其他代码发送传感器数据。到目前为止没有问题。但是对于电池我因为registerreceiver()函数而无法做到这一点。由于我没有使用活动和onCreate方法,我可以在收到短信时发送电池数据吗?
当我尝试在broadcastreceiver的onreceive()方法中调整它时,我的应用程序崩溃了。我在任何地方都搜索了一个活动。我想只在广播接收器内做所有事情。
我的代码是这样的:
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
}
}
}
答案 0 :(得分:7)
在使用Context
更改粘性电池Context
时,请尝试使用应用registerReceiver()
代替广播接收器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);
}