Android 地图不';在我第二次打开活动后加载

Android 地图不';在我第二次打开活动后加载,android,android-activity,maps,Android,Android Activity,Maps,我有一个地图活动,显示用户所在的位置。当我第一次打开该活动时,一切都很好。但是如果我去其他地方再次打开该活动,它不会显示用户所在的位置,只是欧洲地图(因此我猜它不识别gps)。这是这个的代码 public class Map extends AppCompatActivity implements LocationListener, OnMapReadyCallback, NavigationView.OnNavigationItemSelectedListener { @Override

我有一个地图活动,显示用户所在的位置。当我第一次打开该活动时,一切都很好。但是如果我去其他地方再次打开该活动,它不会显示用户所在的位置,只是欧洲地图(因此我猜它不识别gps)。这是这个的代码

public class Map extends AppCompatActivity implements LocationListener, OnMapReadyCallback, NavigationView.OnNavigationItemSelectedListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    toolbar.setTitle("");
    toolbar.setSubtitle("");

    //gpsStatus = (TextView) findViewById(R.id.gpsStatus);
    dis = (TextView) findViewById(R.id.distancePreview);

    newActL = (RelativeLayout) findViewById(R.id.newAct);
    startActL = (RelativeLayout) findViewById(R.id.Act);
    timer = (TextView) findViewById(R.id.timePreview);

    newActB = (ImageButton) findViewById(R.id.newActB);
    stopActB = (ImageButton) findViewById(R.id.stopActB);
    gearMap = (ImageButton) findViewById(R.id.gearmap);

    List<String> spinnerArray = new ArrayList<String>();
    spinnerArray.add("Arrival");
    spinnerArray.add("Departure");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, spinnerArray);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sItems = (Spinner) findViewById(R.id.ardep);
    sItems.setAdapter(adapter);

    List<String> spinnerArray2 = new ArrayList<String>();
    spinnerArray2.add("Running");
    spinnerArray2.add("Walking");
    spinnerArray2.add("Cycling");
    spinnerArray2.add("Roller skating");
    spinnerArray2.add("Skateboarding");
    spinnerArray2.add("Kickbiking");
    spinnerArray2.add("Teleporting");

    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, spinnerArray2);

    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    actCombo = (Spinner) findViewById(R.id.actCombo);
    actCombo.setAdapter(adapter2);
    Calendar rightNow = Calendar.getInstance();
    int currentHour = rightNow.get(Calendar.HOUR_OF_DAY);
    if (currentHour < 12) {
        actCombo.setSelection(GetInfo.arract - 1);
        sItems.setSelection(0);
    } else {
        actCombo.setSelection(GetInfo.depact - 1);
        sItems.setSelection(1);
    }

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            3000,
            1, this);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview);
    mapFragment.getMapAsync(this);
    line = new PolylineOptions().width(3).color(Color.BLUE);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    headerView = navigationView.getHeaderView(0);
    fullnameside = (TextView) headerView.findViewById(R.id.fullnameside);
    emailside = (TextView) headerView.findViewById(R.id.emailside);
    fullnameside.setText("" + GetInfo.fullname);
    emailside.setText("" + GetInfo.email);

    navigationView.setNavigationItemSelectedListener(this);
    navigationView.getMenu().getItem(1).setChecked(true);

}

private void getLocation() {
    //Toast.makeText(getBaseContext(), "e paides", Toast.LENGTH_SHORT).show();
    String locationProvider = LocationManager.NETWORK_PROVIDER;
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location location = locationManager.getLastKnownLocation(locationProvider);
    try {
        line.add(new LatLng(location.getLatitude(), location.getLongitude()));
        GMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title(""));
        GMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16.0f));
        steps++;
        getloc = true;
    } catch (NullPointerException e) {
        //Toast.makeText(this.getBaseContext(), "gyhg" + e.toString(), Toast.LENGTH_LONG).show();
    }
}

public void StopTrip(View v) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Stop and upload your trip?")
            .setCancelable(true)
            .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    float finalDistance = (float) (distance / 1000.0);
                    if (finalDistance < 2) {
                        Intent myIntent = new Intent(Map.this, Profile.class);
                        startActivity(myIntent);
                    }
                    TimeBuff += MillisecondTime;
                    handler.removeCallbacks(runnable);
                    // newActL.setVisibility(View.GONE);
                    // startActL.setVisibility(View.VISIBLE);
                    enabledActivity = false;
                    //database post
                    try {
                        Date currentTime = Calendar.getInstance().getTime();
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        String formattedDate = df.format(currentTime);

                        Date currentTime2 = Calendar.getInstance().getTime();
                        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd%20HH:mm:ss");
                        dateActEnd = df2.format(currentTime2);

                        String act = actCombo.getSelectedItem().toString();
                        String act_id = "7";
                        switch (act) {
                            case "Running":
                                act_id = "1";
                                break;
                            case "Walking":
                                act_id = "2";
                                break;
                            case "Cycling":
                                act_id = "3";
                                break;
                            case "Roller skating":
                                act_id = "4";
                                break;
                            case "Skateboarding":
                                act_id = "5";
                                break;
                            case "Kickbiking":
                                act_id = "6";
                                break;
                            case "Teleporting":
                                act_id = "7";
                                break;
                        }

                        String direcor;
                        if (sItems.getSelectedItem().toString().equals("Arrival"))
                            direcor = "arrival";
                        else
                            direcor = "departure";

                        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                        StrictMode.setThreadPolicy(policy);
                        // String.format("%.1f", finalDistance)
                        URL obj = new URL("https://gekon.technologypark.cz/api/v1/record/create?user=" + LoginInfo.UserID
                                + "&date=" + formattedDate + "&distance=" + String.format("%.1f", finalDistance) + "&direction=" + direcor
                                + "&activity=" + act_id + "&polyline=" + PolyUtil.encode(line.getPoints()) + "&start=" + dateActStart
                                + "&end=" + dateActEnd + "&source=mobileapp");

                        HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
                        conn.setRequestMethod("POST");
                        conn.setRequestProperty("ApiSecret", LoginInfo.ApiSecret);
                        conn.connect();
                        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
                        StringBuilder sb = new StringBuilder();
                        String output;
                        while ((output = br.readLine()) != null)
                            sb.append(output);

                        JSONObject jsonObj = new JSONObject(sb.toString());
                        JSONObject curRecord = new JSONObject(jsonObj.getString("data"));

                        Trips.datet.add(currentTime);
                        Trips.datestr.add(formattedDate);
                        Trips.act.add(act_id);
                        Trips.tripType.add(sItems.getSelectedItem().toString());
                        Trips.dist.add(String.format("%.1f", finalDistance));
                        Trips.trip_ids.add(curRecord.getString("trip_id"));
                        Trips.calc(++Trips.points);

                        TripsCalendarInfo.datet.add(currentTime);
                        TripsCalendarInfo.act.add(act_id);
                        TripsCalendarInfo.act_str.add(act);
                        TripsCalendarInfo.tripType.add(sItems.getSelectedItem().toString());
                        TripsCalendarInfo.dist.add(String.format("%.1f", finalDistance));
                        TripsCalendarInfo.datestr.add(formattedDate);
                        TripsCalendarInfo.trip_ids.add(curRecord.getString("trip_id"));
                        TripsCalendarInfo.trip_source.add("mobileapp");
                        TripsCalendarInfo.polyline.add(PolyUtil.encode(line.getPoints()));
                        TripsCalendarInfo.CanItrip();

                        float km_up = Float.parseFloat(TripsInfo.km.get(TripsInfo.userRank - 1)) + finalDistance;
                        int trip_up = Integer.parseInt(TripsInfo.trips.get(TripsInfo.userRank - 1)) + 1;
                        TripsInfo.trips.set(TripsInfo.userRank - 1, "" + trip_up);
                        TripsInfo.km.set(TripsInfo.userRank - 1, String.format("%.1f", km_up));
                        TripsInfo.rankSort();

                        getloc = false;

                        Intent myIntent = new Intent(Map.this, Profile.class);
                        startActivity(myIntent);

                    } catch (Exception e) {
                        Toast.makeText(getBaseContext(), "Error upload, please check your options at gear button", Toast.LENGTH_LONG).show();
                    }
                }
            });
    float finalDistance = (float) (distance / 1000.0);
    if (finalDistance < 2) {
        builder.setMessage("Your trip is below 2km and it will not be counted.\nAre you sure you want to stop?");
    }
    AlertDialog alert = builder.create();
    alert.show();

}


public void StartAct(View v) {
    timer.setVisibility(View.VISIBLE);
    dis.setVisibility(View.VISIBLE);
    newActB.setVisibility(View.GONE);
    stopActB.setVisibility(View.VISIBLE);
    //gearMap.setVisibility(View.GONE);
    navigationView.setVisibility(View.GONE);
    //headerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    toolbar.setNavigationIcon(null);          // to hide Navigation icon
    //toolbar.setDisplayHomeAsUpEnabled(false);
    handler = new Handler();

    Date currentTime = Calendar.getInstance().getTime();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd%20HH:mm:ss");
    dateActStart = df.format(currentTime);
    StartTime = SystemClock.uptimeMillis();
    handler.postDelayed(runnable, 0);

    startService(new Intent(this, LocationService.class));
    //enabledActivity = true;
}

public void ChangeAct(View v) {
    if (newActL.getVisibility() == View.GONE) {
        newActL.setVisibility(View.VISIBLE);
        startActL.setVisibility(View.GONE);
    } else {
        newActL.setVisibility(View.GONE);
        startActL.setVisibility(View.VISIBLE);
    }
}

public Runnable runnable = new Runnable() {

    public void run() {

        MillisecondTime = SystemClock.uptimeMillis() - StartTime;

        UpdateTime = TimeBuff + MillisecondTime;

        Seconds = (int) (UpdateTime / 1000);

        Minutes = Seconds / 60;

        Hours = Minutes / 60;

        Seconds = Seconds % 60;

        Minutes = Minutes % 60;

        MilliSeconds = (int) (UpdateTime % 100);

        timer.setText("Time: " + String.format("%02d", Hours) + ":"
                + String.format("%02d", Minutes) + ":"
                + String.format("%02d", Seconds));

        handler.postDelayed(this, 950);
    }

};

@Override
public void onMapReady(GoogleMap map) {
    GMap = map;
    GMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(49.8117806, 15.6970293), 6.0f));
    if (!getloc)
        getLocation();
}
公共类映射扩展AppCompatActivity实现LocationListener、OnMapReadyCallback、NavigationView.OnNavigationItemSelectedListener{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
getWindow().addFlags(WindowManager.LayoutParams.FLAG\u保持屏幕打开);
toolbar=(toolbar)findviewbyd(R.id.toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayShowTitleEnabled(false);
工具栏。设置标题(“”);
工具栏。设置子标题(“”);
//gpsStatus=(TextView)findViewById(R.id.gpsStatus);
dis=(文本视图)findViewById(R.id.distancePreview);
newActL=(RelativeLayout)findViewById(R.id.newAct);
startActL=(RelativeLayout)findViewById(R.id.Act);
计时器=(TextView)findViewById(R.id.timePreview);
newActB=(ImageButton)findViewById(R.id.newActB);
stopActB=(ImageButton)findViewById(R.id.stopActB);
gearMap=(ImageButton)findViewById(R.id.gearMap);
List spinnerray=new ArrayList();
SpinneArray.添加(“到达”);
Spinnerray.添加(“离开”);
ArrayAdapter适配器=新的ArrayAdapter(
这个,android.R.layout.simple\u微调器\u项目,微调阵列);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
sItems=(微调器)findviewbyd(R.id.ardep);
setAdapter(适配器);
List spinnerray2=新的ArrayList();
spinnerray2.添加(“运行”);
spinnerray2.添加(“行走”);
添加(“循环”);
添加(“轮滑”);
添加(“滑板”);
spinnerray2.添加(“踢自行车”);
添加(“传送”);
ArrayAdapter适配器2=新的ArrayAdapter(
这个,android.R.layout.simple_微调器_项,spinnerray2);
adapter2.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
actCombo=(微调器)findViewById(R.id.actCombo);
actCombo.setAdapter(适配器2);
Calendar rightNow=Calendar.getInstance();
int currentHour=rightNow.get(Calendar.HOUR/OF\u DAY);
如果(当前小时<12){
actCombo.setSelection(GetInfo.arract-1);
站点设置选择(0);
}否则{
actCombo.setSelection(GetInfo.depact-1);
选举地点(1);
}
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予&&
ActivityCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u\u位置)!=PackageManager.permission\u已授予){
回来
}
locationManager.RequestLocationUpdate(locationManager.GPS\U提供程序,
3000,
1,这),;
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapview);
getMapAsync(这个);
line=新的PolylineOptions().width(3.color)(color.BLUE);
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
ActionBarDrawerToggle切换=新建ActionBarDrawerToggle(
这,抽屉,工具栏,R.string.navigation\u drawer\u open,R.string.navigation\u drawer\u close);
抽屉。添加抽屉链接器(切换);
toggle.syncState();
navigationView=(navigationView)findViewById(R.id.nav_视图);
headerView=navigationView.getHeaderView(0);
fullnameside=(TextView)headerView.findViewById(R.id.fullnameside);
emailside=(TextView)headerView.findViewById(R.id.emailside);
fullnameside.setText(“+GetInfo.fullname”);
emailside.setText(“+GetInfo.email”);
navigationView.setNavigationItemSelectedListener(此);
navigationView.getMenu().getItem(1).setChecked(true);
}
私有void getLocation(){
//Toast.makeText(getBaseContext(),“e paides”,Toast.LENGTH_SHORT.show();
字符串locationProvider=LocationManager.NETWORK\u PROVIDER;
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于ActivityCompat,请请求权限以获取更多详细信息。
回来
}
Location Location=locationManager.getLastKnownLocation(locationProvider);
试一试{
add(新LatLng(location.getLatitude(),location.getLongitude());
GMap.addMarker(新的MarkerOptions().position(新的LatLng(location.getLatitude(),location.getLongitude()).title(“”);
animateCamera(CameraUpdateFactory.newLatLngZoom(新LatLng(location.getLatitude(),location.getLength()),16.0f));
steps++;
getloc=true;
}捕获(NullPointerException e){
//Toast.makeText(this.getBaseContext(),“gyhg”+e.toString(),Toast.LENGTH_LONG).show();
}
}
公共停车场(视图五){
AlertDialog.Builder=新建AlertDialog.Builder(此);
setMessage(“停止并上传您的行程?”)
.setCancelable(真)
.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
@凌驾