【发布时间】:2019-08-17 06:17:29
【问题描述】:
我的应用使用后台服务来监听 GPS 位置变化。它适用于 android 7.1.1(即使我的活动已关闭或屏幕已关闭)。当我在 android 9(使用 AVD)上尝试它时,它仅在活动处于前台时才有效,否则无效。当应用程序关闭时,服务似乎已停止。 minSdkVersion 设置为 23。为什么应用程序在不同的 API 上有不同的行为? Android 不保证应用程序在较新的操作系统版本上的兼容性?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**code to request permission*/
startService(new Intent(this, EventControllerService.class));
}
}
public final class EventControllerService extends Service {
private final static int MIN_TIME_FOR_GPS_UPDATES = 1;
private final static float MIN_METER_FOR_GPS_UPDATES = 10.0f;
private static NotificationManager mNotificationManager;
private final BroadcastReceiver receiverDNDChange = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//work on receive
}
};
private final EventControllerService self = this;
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Toast.makeText(self, "DENTRO", Toast.LENGTH_LONG).show();
try {
//work to do on position change
} catch (SecurityException ignored) {
stopSelf();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
@Override
public void onCreate() {
super.onCreate();
try {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
IntentFilter filter = new IntentFilter();
filter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
registerReceiver(receiverDNDChange, filter);
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_FOR_GPS_UPDATES, MIN_METER_FOR_GPS_UPDATES, locationListener);
} catch (SecurityException ignored) {
stopSelf();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="marcomantovani.pcd.silentmap">
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<service android:name=".EventControllerService"/>
<receiver android:name="marcomantovani.pcd.silentmap.AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如何更改代码以避免此问题?
【问题讨论】:
标签: android android-service android-location android-broadcastreceiver