Search code examples
androidandroid-intentandroid-broadcastandroid-gps

Getting app name that broadcast a intent


I'm developing an app that tracks the usage of the GPS (not only if the GPS was turned on/off but the location requests).

After a research I find out that I can archive this by using the following code in my manifest.

<receiver android:name=".triggers.TriggerGPS">
    <intent-filter>
        <action android:name="android.location.GPS_ENABLED_CHANGE" />
    </intent-filter>
</receiver>

This is working, every time that an app request the GPS coordinates my TriggerGPS class is invoked.

public class TriggerGPS extends BroadcastReceiver {

    Calendar c = Calendar.getInstance();

    @Override
    public void onReceive(Context context, Intent intent) {

            String currentDatetime = c.get(Calendar.DAY_OF_MONTH) + "/" + c.get(Calendar.MONTH) + "/" +
                    c.get(Calendar.YEAR) + " " + c.get(Calendar.HOUR_OF_DAY) + ":" +
                    c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);

            Log.d("---------log--------", "Intent ( " + intent.getAction() + " ) GPS Status onReceive : " + currentDatetime);
    }
}

But my app should create a report indicating each app is using the GPS and how many times the app used the GPS to get de coordinates per day.

The problem here is that I can't figure out how to get the app name that broadcast the event.

There's any way to do that?

Thank you all.


Solution

  • The problem here is that I can't figure out how to get the app name that broadcast the event.

    Apps do not broadcast that Intent. The system broadcasts that Intent. You have no way of determining why the user changed their device settings to enable or disable a location provider. After all, there does not have to be a specific app-driven reason. The user could simply elect to tap on the tile in the notification shade, go into Settings and toggle the provider state, or otherwise change the state just for giggles.