如何在最小化应用程序的同时保持其运行(地图)

时间:2018-10-30 12:54:12

标签: android service maps

我有一个类显示从旅程开始到结束的用户路径。如果我最小化应用程序并打开它以停止旅程记录,它将向我显示从旅程开始到结尾的一行正是我走的路。这里是代码

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

   //variables

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

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    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);

    //Toast.makeText(this.getBaseContext(), ""+ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION), Toast.LENGTH_SHORT).show();
}

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();
}

我尝试了this,但是没有用。我认为我需要以某种方式创建服务并将所有这些代码放入其中,但是我不知道如何。

3 个答案:

答案 0 :(得分:2)

这里的服务很合适,但是
从Android O开始,引入了一些背景限制:

  • Location更新仅在您的应用程序处于前台,正在运行foreground service或被另一个前台应用程序(如IME或通知侦听器)使用时接收。
  • 后台服务现在也有limitations

基本上,出于您的目的,您可以将基于位置的业务逻辑移至某个服务,将其作为前台服务启动(请参阅Context.startForegroundService()Service.startForeground())并显示一个{{3} },对此也可以使用notification
尽管不是必需的,但最好是让用户通过与通知进行交互来停止应用程序的服务或将应用程序置于前台,您可以在此tutorial上阅读更多内容。

请注意,Android P要求您的应用持有here权限才能启动前台服务。

此外,您可能想看看FOREGROUND_SERVICE

答案 1 :(得分:1)

要保持您的应用运行,您应该创建一个服务。如果您不创建服务,则Android会将您的应用识别为入侵性应用,并尝试将其关闭。

看看Android Service

答案 2 :(得分:1)

在以下步骤中显示从开始到结束流程的路径。

  • 创建服务(从活动的onCreate()开始)。 服务将开始位置跟踪和停止位置跟踪。

onLocationChanged():-获取所有位置坐标并存储在某个地方。

使用此位置坐标显示路径。

  • 在活动销毁之前停止服务。