如何在应用最小化时保持应用运行(地图)



我有一个类,它显示了从旅程开始到结束的用户路径。如果我最小化应用程序并打开它来停止行程记录,它会显示从行程开始到结束的一行,而不是我所走的路线。这是代码

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

我试过了,但没用。我相信我需要以某种方式创建一个服务,并将所有这些代码放在里面,但我不知道怎么做。

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

  • 只有当您的应用程序在前台、运行前台服务或被其他前台应用程序(如IME或Notification监听器(使用时,才能接收位置更新
  • 后台服务现在也有局限性

基本上,出于您的目的,您可以将基于位置的业务逻辑移动到服务,将其作为前台服务启动(请参阅Context.startForegroundService((和Service.startForeground(((并显示通知,还有一个关于这方面的OK教程
尽管不是必需的,但让用户停止应用程序的服务或通过与通知交互将应用程序带到前台可能是个好主意,您可以在此处阅读更多信息。

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

此外,您可能还想看看地理围栏API。

要保持应用程序的运行,您应该创建一个服务。如果你不创建服务,Android会识别出你的应用程序具有入侵性,并尝试关闭它。

看看安卓服务

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

  • 创建服务(在活动的onCreate((中启动(。服务将启动位置跟踪并停止位置跟踪

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

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

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

相关内容

最新更新