我正在开发一个Android应用程序,它具有在用户启用或禁用GPS时应用程序运行的功能。我收到广播并使用界面显示活动的状态。
GpsLocationReceiver
public class GpsLocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(context, "GPS Disabled", Toast.LENGTH_SHORT).show();
}
}
我在Manifest.xml中注册了这个BroadcastReceiver
<receiver android:name=".GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
答案 0 :(得分:1)
活动
public void onResume() {
registerReceiver(mGattUpdateReceiver, new IntentFilter().addAction("ACTION_DATA_AVAILABLE");
return intentFilter;);
}
public void onPause(){
unregisterReceiver(mGattUpdateReceiver);
}
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action.equals("ACTION_DATA_AVAILABLE")) {
}
}
};
2. 在BroadcastReciver onReciever()
final Intent intent = new Intent("ACTION_DATA_AVAILABLE");
intent.putExtra("KEY","YourString");
sendBroadcast(intent);
答案 1 :(得分:0)
第1步
private void sendLocationBroadcast(Intent intent) {
SharedPreferences pref = getSharedPreferences(Data.SHARED_PREF_NAME, 0);
totaldisplacement = Double.parseDouble(pref.getString(Data.SP_TOTAL_DISTANCE_COVER_TILL, ""));
intent.putExtra(Data.SP_TOTAL_DISTANCE_COVER_TILL, totaldisplacement);
intent.putExtra("latitude", currentlat);
intent.putExtra("longitude", currentlong);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Log.e("Data", "" + rikshawspeed + " " + currentlat);
}
第2步
private void dataSendToMainActivity() {
Intent intent = new Intent("speedExceeded");
sendLocationBroadcast(intent);
}
第3步
public BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
distanceFromService = intent.getDoubleExtra(Data.SP_TOTAL_DISTANCE_COVER_TILL, 55);
Double currentLatitude = intent.getDoubleExtra("latitude", 0);
Double currentLongitude = intent.getDoubleExtra("longitude", 0);
// Toast.makeText(getApplicationContext(), "speed " + distanceFromService + "\n" + "Lat Long " + "\n" + currentLatitude + "\n" + currentLongitude, Toast.LENGTH_SHORT).show();
// ... react to local broadcast message
Log.e("Main", "" + distanceFromService);
}
};
第4步
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("speedExceeded"));