0

I'm developing an Android application and it has a functionality that while the application is running if user enable or disable GPS. I received the Broadcast and display the status on an activity using Interface.

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();
    }


}

I registered this BroadcastReceiver in Manifest.xml

<receiver android:name=".GpsLocationReceiver">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
Anil
  • 1,087
  • 1
  • 11
  • 24
Rehan Sarwar
  • 994
  • 8
  • 20

2 Answers2

1
  1. In Activity

     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.In BroadcastReciver onReciever()

    final Intent intent = new Intent("ACTION_DATA_AVAILABLE");
    intent.putExtra("KEY","YourString");
    sendBroadcast(intent);
    
Rajasekhar
  • 2,345
  • 1
  • 13
  • 20
0

Step 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);
}

Step 2

 private void dataSendToMainActivity() {
    Intent intent = new Intent("speedExceeded");
    sendLocationBroadcast(intent);
}

Step 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);
    }
};

Step 4

 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("speedExceeded"));
Ahmad
  • 201
  • 2
  • 12