android – Google Map API v2在重新启动应用后不会更新地图

android – Google Map API v2在重新启动应用后不会更新地图,第1张

概述我使用谷歌地图Api V2在 Android上编写了一个应用程序.该应用程序基本上运行一个服务,并使用SupportMapFragment向使用SupportMapFragment添加Google Map的Activity报告位置. 当我启动应用程序时,一切似乎都运行正常,地图也会更新.问题是当我按下按钮或屏幕关闭并返回App谷歌地图停止工作.仍然会报告位置,我在Map对象上调用moveCamer 我使用谷歌地图API V2在 Android上编写了一个应用程序.该应用程序基本上运行一个服务,并使用SupportMapFragment向使用SupportMapFragment添加Google Map的Activity报告位置.

当我启动应用程序时,一切似乎都运行正常,地图也会更新.问题是当我按下按钮或屏幕关闭并返回App谷歌地图停止工作.仍然会报告位置,我在Map对象上调用moveCamera方法.

public class GpsTracker extends androID.support.v4.app.FragmentActivity {public static final int GPS_LOCATION_UPDATE = 0;public static final int NETWORK_LOCATION_UPDATE = 1;public static final int LOCATION_NETWORK_UNAVAILABLE = 2;public static final int LOCATION_GPS_UNAVAILABLE = 3;private static String gpsTrackerTag = "GpsTracker:MainActivity";private GoogleMap mMap;private polylineoptions mpolyOptions = new polylineoptions();GpsTrackerMyService myservice = null;ServiceConnection sc = null;static MyHandlerCallback myCallback = null;//private Boolean mServiceRunning = false; // Initial State When service is yet not started.private Boolean mServiceBinded = false;public static Callback CallBackHandler(){    return myCallback;}@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_gps_tracker);    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()            .findFragmentByID(R.ID.map);    if (savedInstanceState == null)     {        // First incarnation of this activity.        mapFragment.setRetainInstance(true);    } else {        // Reincarnated activity. The obtained map is the same map instance in the prevIoUs        // activity life cycle. There is no need to reinitialize it.        mMap = mapFragment.getMap();    }    InitializeActivity();    setUpMAPIfNeeded();}@OverrIDeprotected voID onPause(){    super.onPause();    if(mServiceBinded)    {        unbindService(sc);        mServiceBinded = false;        Log.d(gpsTrackerTag,"onPause(): Unbinded from the Serivce");    }    else        Log.d(gpsTrackerTag,"onPause():  Service Already in not Bind State");}@OverrIDeprotected voID onResume(){    super.onResume();    if(GpsTrackerMyService.isinstanceCreated() == true)    {        bindService(new Intent(this,GpsTrackerMyService.class),sc,0);        mServiceBinded = true;        Log.d(gpsTrackerTag,"onResume(): Binded to the Service");    }    else        Log.d(gpsTrackerTag,"onResume(): Service Not Running");    setUpMAPIfNeeded();}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.activity_gps_tracker,menu);    return true;}private voID setUpMAPIfNeeded() {    // Do a null check to confirm that we have not already instantiated the map.    if (mMap == null) {        // Try to obtain the map from the SupportMapFragment.        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentByID(R.ID.map))                .getMap();        // Check if we were successful in obtaining the map.        if (mMap != null) {            setUpMap();        }    }}private voID setUpMap() {   // mMap.addMarker(new MarkerOptions().position(new LatLng(0,0)).Title("Marker"));}private voID InitializeActivity() {    if(myCallback == null)        myCallback = new MyHandlerCallback();    if(sc == null){        sc = new ServiceConnection() {            @OverrIDe            public voID onServicedisconnected(Componentname name) {                mServiceBinded = false;                CharSequence text = "Service disConnected";                Toast t = Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG);                t.show();                Log.d(gpsTrackerTag,"Service disconnected ");            }            @OverrIDe            public voID onServiceConnected(Componentname name,IBinder service) {                myservice = ((mybinder) service).getService();                mServiceBinded = true;                Log.d(gpsTrackerTag,"Service Connected ");            }        };    }   }public voID startbutton_Click(VIEw vIEw){    if(GpsTrackerMyService.isinstanceCreated() == false)    {        Intent serviceIntent = startService();        bindService(serviceIntent);        Log.d(gpsTrackerTag,"startbutton_Click(): Service Started and Called for bind");    }    else        Log.d(gpsTrackerTag,"startbutton_Click(): Service Already Running");}private voID bindService(Intent serviceIntent) {    bindService(serviceIntent,0 );}private Intent startService() {    Intent serviceIntent = new Intent(this,GpsTrackerMyService.class);    startService(serviceIntent);    return serviceIntent;}public voID stopbutton_Click(VIEw vIEw){    stopService();}private voID stopService() {    if(GpsTrackerMyService.isinstanceCreated() == true)    {        Log.d(gpsTrackerTag,"stopService(): Service Running Calling Stop on the Service");        myservice.stopSelf();    }    else        Log.d(gpsTrackerTag,"stopService(): Service Already Stopped");}public class MyHandlerCallback implements Callback{    @OverrIDe    public boolean handleMessage(Message arg0) {        Location l;        CharSequence text;        Toast t;        LatLng mLatLng;        int pos = 1;        switch(arg0.what)        {        case GpsTracker.LOCATION_NETWORK_UNAVAILABLE:            text = "Network Location Unavailable";            t = Toast.makeText(getApplicationContext(),Toast.LENGTH_LONG);            t.show();            return true;        case GpsTracker.NETWORK_LOCATION_UPDATE:            l = ((Location)arg0.obj);            mLatLng = new LatLng(l.getLatitude(),l.getLongitude());            mpolyOptions.add(mLatLng).wIDth(2).color(color.BLACK).geodesic(true);            mMap.addpolyline(mpolyOptions);            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLng,12));            mMap.addMarker(new MarkerOptions()               .position(mLatLng)               .Title("Pos " + pos));            //text =  ((Location)arg0.obj).toString();            t = Toast.makeText(getApplicationContext(),"Network Location Update",Toast.LENGTH_LONG);            t.show();            return true;        case GpsTracker.GPS_LOCATION_UPDATE:            l = ((Location)arg0.obj);            mLatLng = new LatLng(l.getLatitude(),l.getLongitude());            //mpolyOptions.add(mLatLng).wIDth(2).color(color.BLACK).geodesic(true);            //mMap.addpolyline(mpolyOptions);            boolean check = mMap.isMyLocationEnabled();            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLng,12));                    //text =    ((Location)arg0.obj).toString();            t = Toast.makeText(getApplicationContext(),"GPS Location Update",Toast.LENGTH_LONG);            t.show();            return true;        case GpsTracker.LOCATION_GPS_UNAVAILABLE:            text = "Gps Location Unavailable";            t = Toast.makeText(getApplicationContext(),Toast.LENGTH_LONG);            t.show();            return true;            default:                return false;        }    }}

在MyCallBackHandler案例GPS_LOCATION_UPDATE中,地图不会移动相机.

解决方法 我认为你的问题与非静态内部类CallBack有关.重新创建活动时,在myCallBack中保留对它的静态引用,myCallBack本身具有对旧活动的隐式引用.这不仅是内存泄漏,而且当您调用CallBack时,它正在查看旧活动的变量.有关内存泄漏和非静态内部类的更多信息,请参见 this post.

如果在启动新活动时进行新的回调,则应解决问题.因此,在InitializeActivity中,进行更改

if(myCallback == null)        myCallback = new MyHandlerCallback();

myCallback = new MyHandlerCallback();
总结

以上是内存溢出为你收集整理的android – Google Map API v2在重新启动应用后不会更新地图全部内容,希望文章能够帮你解决android – Google Map API v2在重新启动应用后不会更新地图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1124828.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存