这是您明智的答案:
1)您可以使用 LocationManager 获取设备的位置。
LocationManager locationManager = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
makeUseOfNewLocation(location); //here you get the new location
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
您还可以使用 GeoCoder 类使用从 locationManager 接收到的经度和纬度来准确指出地址。详细教程在这里 - LocationManager
2)您可以安排要调用的特定函数 - 使用 BroadcastReceiver 和 AlarmManager 服务。
私人无效setRecurringAlarm(上下文上下文){
// we know mobiletuts updates at right around 1130 GMT.
// let's grab new stuff at around 11:45 GMT, inexactly
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getTimeZone("GMT"));
updateTime.set(Calendar.HOUR_OF_DAY, 11);
updateTime.set(Calendar.MINUTE, 45);
Intent downloader = new Intent(context, AlarmReceiver.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) getActivity().getSystemService(
Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, recurringDownload);
}
详细教程BroadCastReceiver 和 AlarmManager
因此,如果您愿意,您可以在 BroadCastReceiver 的 OnReceive 中反复检查设备的当前位置(使用 LocationManager,如第 1 点所示)。
3)当 BatteryLevel 发生如下变化时,有一个 BroadCastReceiver 可以捕获:
private BroadcastReceiver mBatInfoReceiver
= new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
int level = intent.getIntExtra("level", 0);
// do something...
}
}
registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
对于插件或充电使用以下代码:
// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
// How are we charging?
int chargePlug = battery.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;
我希望这一切对你有帮助......