Excel2016提示内存不足解决办法-英雄云拓展知识分享
377
2024-01-22
该服务的目标是更新用户在后台确当前位置,我不希望有前景服务。然后,每50个位置都会写一个GMX文件。
我面对的问题是,在随机数量的更新(通常是在生成50个位置点之前)以后,该服务没有明确的缘由而死,然后再开始服务。 ondestroy不会在任什么时候候被调用。
这是服务代码(使用StarterVice启动):
public class LocationService extends Service {private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private static LocationManager locationManager;
private final class ServiceHandler extends android.os.Handler{

private LocationManager locationManager;
public ServiceHandler(Looper looper){
super(looper);
}
@Override
public void handleMessage(Message msg) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
run(locationManager);
}
}
@Override
public void onCreate() {
HandlerThread thread = new HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
Log.i("Service","onHandleIntent lancé");
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
Log.i("Service","onHandleIntent lancé");
return START_STICKY; //permet de redémarrer le service s'il est tué
}
@Override
public void onDestroy() {
Log.i("DESTROY","IsDestroyed");
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected void run(LocationManager locationManager){
Log.i("Service","Service lancé");
boolean hasLocationFeature = checkLocationFeature();
if ( hasLocationFeature && (ACCESS_FINE_LOCATION == PackageManager.PERMISSION_GRANTED || ACCESS_COARSE_LOCATION == PackageManager.PERMISSION_GRANTED ) ) {
sLocation(locationManager);
}
}
protected void sLocation(LocationManager locationManagerArg){
final List<Location> locations = new ArrayList<>();
final LocationManager locationManager = locationManagerArg;
Log.i("sLocation","lancé");
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
saveNewLocation(location);
if (location != null) {
locations.add(location);
Log.i("Locationsize", String.valueOf(locations.size()));
if(locations.size()%50 == 0) {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + System.currentTimeMillis() + ".gpx");
GPXWriter(file, "test1", locations);
locations.clear();
}
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,0,locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,5000,0,locationListener);
}
编写GPX文件的方法没有问题。我已在一个摹拟器和两个实际装备上对此进行了测试,一样的问题也会产生。它可能与记忆压力无关。我可以在手机上使用其他利用程序运行超过100个小时的利用程序服务。
这项服务为何要死,我该怎样办?谢谢
由于内存限制,需要更多的资源,装备上的电池节省配置文件,乃至在Android o中,OS杀死的操作系统可以随即杀死您的服务,而OS Kill只会在您的利用程序进入后台时杀死您的服务。您无能为力地“保证”您的服务永久存在。
创建前景服务是具有长途服务的最好选择
免责声明:
本网址(www.yingxiongyun.com)发布的材料主要源于独立创作和网友匿名投稿。此处提供的所有信息仅供参考之用。我们致力于提供准确且可信的信息,但不对材料的完整性或真实性作出任何保证。用户应自行验证相关信息的正确性,并对其决策承担全部责任。对于由于信息的错误、不准确或遗漏所造成的任何损失,本网址不承担任何法律责任。本网站所展示的所有内容,如文字、图像、标志、音频、视频、软件和程序等的版权均属于原创作者。如果任何组织或个人认为网站内容可能侵犯其知识产权,或包含不准确之处,请即刻联系我们进行相应处理。
发表评论
暂时没有评论,来抢沙发吧~