java—尝试使用单例、服务和警报将用户的位置加载到Map上

au9on6nz  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(398)

到目前为止,我已经设置了一个闹钟,每隔一段时间醒来,然后获取用户的纬度和经度。它存储在singleton类中 Position 它将被加载到不同的活动中,并根据上次记录的位置作为标记放置在Map上。
目标我希望应用程序跟踪用户的位置,即使它是关闭的,至于为什么我使用了 AlarmManager 在一定时间后唤醒设备并获取坐标,直到他们请求停止。
即使类返回先前录制的 Position 值,即坐标,调用它以在Map上放置位置的活动返回坐标(0.0,0.0),即null,假设它正在初始化对象,就好像它不存在一样。
有什么问题吗?
位置-单子

public class Position {

    private double latitude;
    private double longitude;
    private String coordinate;
    private String service;
    private static Position instance;

    private Position() {

    }

    public String getCoordinate() {
        return coordinate;
    }

    public void setCoordinate(String coordinate) {
        this.coordinate = coordinate;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public static Position getInstance() {
        if (instance == null) {
            instance = new Position();
        }
        return instance;
    }
}

警报-跟踪位置的位置

public class Alarm extends BroadcastReceiver {

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

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        // get the service code
        String serviceCode = intent.getStringExtra("serviceCode");

        // get the last known location
        LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);

        //store the latitude and longitude locally
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        String coordinate = latitude + ", " + longitude;

        Position pos = Position.getInstance();
        Log.d("", "Previous position: " + pos.getCoordinate());

        pos.setLatitude(latitude);
        pos.setLongitude(longitude);
        pos.setCoordinate(coordinate);The 
        pos.setService(serviceCode);

        Log.d("", "Found new position: " + pos.getCoordinate());

        wl.release();
    }

    public void SetAlarm(Context context) {
        AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, Alarm.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pi); // Millisec * Second * Minute
    }

    public void CancelAlarm(Context context) {
        Intent intent = new Intent(context, Alarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }
}

位置服务-什么初始化警报

public class LocationService extends android.app.Service {

    Alarm alarm = new Alarm();
    private NotificationManager nm;

    @Override
    public void onCreate() {
        super.onCreate();
        showNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        alarm.SetAlarm(LocationService.this);
        return START_STICKY;
    }

   private void showNotification() {
        // set up notification label to notify user that the app is still tracking the location
        nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        CharSequence text = "Press to stop tracking";

        // don't let user close it, for the purpose of always letting them know the location tracker is still running
        Notification notification = new Notification(R.drawable.fav_buses, text, System.currentTimeMillis());
        notification.flags = Notification.FLAG_NO_CLEAR;

        // for older APIs
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, PushLocationActivity.class), 0);
        notification.setLatestEventInfo(this, "Tracking Your Location", text, contentIntent);

        nm.notify(R.string.notification_id, notification);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        alarm.CancelAlarm(LocationService.this);
        nm.cancel(R.string.notification_id);
        Log.d("", "DESTROYED");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

stopsfragment—发生nullpointerexception的位置

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    stops = new HashMap<>();
    stopList = StopList.getInstance(getActivity()).getStops();

    request = getArguments().getString("request");

    position = Position.getInstance();
    Log.d("", position.getCoordinate());
}
fjnneemd

fjnneemd1#

你不应该遵循这种方法,这将耗尽设备电池。android有特定的api来接收用户的重要位置更改,您应该使用它们。点击以下链接:https://developer.android.com/training/location/receive-location-updates.html
只需调整代码,而不是活动,代码应该从服务执行,并且应该调用服务在引导时执行。

相关问题