我正在开发一个跟踪 GPS 使用情况的应用程序(不仅是打开/关闭 GPS,还有位置请求)。
经过研究,我发现我可以通过在清单中使用以下代码来存档。
<receiver android:name=".triggers.TriggerGPS">
<intent-filter>
<action android:name="android.location.GPS_ENABLED_CHANGE" />
</intent-filter>
</receiver>
这是有效的,每次应用程序请求 GPS 坐标时,都会调用我的 TriggerGPS 类。
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);
}
}
但是我的应用程序应该创建一个报告,表明每个应用程序正在使用 GPS,以及该应用程序每天使用 GPS 获取坐标的次数。
这里的问题是我无法弄清楚如何获取广播事件的应用程序名称。
有没有办法做到这一点?
谢谢你们。