Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在尝试获取用户';使用单例、服务和警报将s位置加载到地图上_Java_Android_Service_Singleton_Alarmmanager - Fatal编程技术网

Java 正在尝试获取用户';使用单例、服务和警报将s位置加载到地图上

Java 正在尝试获取用户';使用单例、服务和警报将s位置加载到地图上,java,android,service,singleton,alarmmanager,Java,Android,Service,Singleton,Alarmmanager,到目前为止,我已经设置了一个闹钟,每隔一段时间就会醒来,然后记录用户的经度和纬度。这存储在singleton类位置中,该位置将加载到不同的活动中,并根据上次记录的位置作为标记放置在地图上 目标 我希望应用程序即使在关闭时也能跟踪用户的位置,比如为什么我使用AlarmManager在一定时间后唤醒设备并获取坐标,直到他们请求停止 问题 即使该类返回先前记录的位置值,即坐标,调用该函数将位置放置在地图上的活动也会返回坐标(0.0,0.0),即null,假设它正在初始化对象,就好像它不存在一样 有什么

到目前为止,我已经设置了一个闹钟,每隔一段时间就会醒来,然后记录用户的经度和纬度。这存储在singleton类
位置中,该位置将加载到不同的活动中,并根据上次记录的位置作为标记放置在地图上

目标 我希望应用程序即使在关闭时也能跟踪用户的位置,比如为什么我使用
AlarmManager
在一定时间后唤醒设备并获取坐标,直到他们请求停止

问题 即使该类返回先前记录的
位置
值,即坐标,调用该函数将位置放置在地图上的活动也会返回坐标(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());
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
stops=newhashmap();
stopList=stopList.getInstance(getActivity()).getStops();
请求=getArguments().getString(“请求”);
position=position.getInstance();
Log.d(“,position.getCoordinate());
}

您不应该采用这种方法,这会耗尽设备电池的电量。Android有特定的API来接收用户的重要位置更改,您应该使用它们。请点击以下链接:


只需调整代码,而不是活动,代码应该从服务执行,并且应该调用服务在引导时执行。

我以前使用过这个,但目标是让应用程序即使在关闭时也能获得位置更新,至于我为什么使用
AlarmManager
。用户更改位置时,即使应用程序已关闭,也会使用该代码通知您的应用程序。查看此代码:只需将代码从活动更改为服务,并在引导时启动服务。