我写了一个服务,我将数据发送到服务器。我的问题在于,在版本4中,当应用程序关闭时,该服务可用于Internet并且数据将被发送但是当我在版本7上测试时当程序关闭时服务没有Internet访问它似乎是操作系统阻止互联网访问。 我该怎么办? 这是我的代码
public class GPSTracker extends Service implements android.location.LocationListener {
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
private PendingIntent pendingIntent;
Location location; // location
double latitude; // latitude
double longitude; // longitude
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 10 meters
private static final long MIN_TIME_BW_UPDATES = G.preferences.getInt(Const.PREF_DRIVER_STATUS_REQUEST_PERIOD, 10000); // 1 minute
protected LocationManager locationManager;
public GPSTracker() {
getLocation();
}
@Override
public int onStartCommand(Intent intent1, int flags, int startId) {
getLocation();
stopSelf();
Intent intentAlarm = new Intent(getApplicationContext(), com.rasef.app.driver.services.GPSTracker.class);
intentAlarm.setAction(Const.SERVICE_STATUS_LOCATION_ACTION);
pendingIntent = PendingIntent.getService(getApplicationContext(), Const.SERVICE_STATUS_LOCATION_REQUEST_CODE, intentAlarm, 0);
G.alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + G.preferences.getInt(Const.PREF_STATUS_REQUEST_PERIOD, 10000), pendingIntent);
return START_STICKY;
}
@SuppressLint("MissingPermission")
public Location getLocation() {
try {
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isGPSEnabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("Gps", latitude + "," + longitude);
}
}
}
if (isNetworkEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("Network", latitude + "," + longitude);
}
}
}
}
if (location != null) {
sendToServerLocation(location);
} else {
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
private void sendToServerLocation(Location location) {
// codes send to server with library retrofit
}
@Override
public void onLocationChanged(Location location) {
Log.i("Change", "Location:" + location.getLatitude() + "," + location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
答案 0 :(得分:0)
将INTERNET权限添加到清单文件中。
你必须添加这一行:
<uses-permission android:name="android.permission.INTERNET" />
在AndroidManifest.xml中的应用程序标记之外