代码之家  ›  专栏  ›  技术社区  ›  Nemi

活动.暂停不总是接到电话

  •  1
  • Nemi  · 技术社区  · 15 年前

    由于某些原因,当我的应用程序被放到后台时,onpause方法并不总是被调用。不是那样就是 MyLocationOverlay 未正确禁用。

    我的应用程序使用谷歌地图和 当前位置 . 启动应用程序时,我会呼叫 MyLocationOverlay.enableMyLocation() onResume 方法。在 onPause 我调用的方法 MyLocationOverlay.disableMyLocation() . 但是,当我按home键或back键进入主屏幕时,GPS将保持运行状态,如带有GPS图标的状态栏顶部所示。

    只有当我使用高级任务杀手杀死它时,gps图标才会消失。

    EDIT3:onPause肯定是被调用的,因为我在其中放了一个Log语句,但是gps图标仍然存在。因此,看起来要么gps服务没有关闭,要么 当前位置 有个窃听器 disableMyLocation() 方法。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/com.tfb"
      package="com.tfb"
      android:versionName="1.2.2" android:versionCode="11">
    <application android:icon="@drawable/globe_search" 
         android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">
        <activity android:name=".activity.GarageSaleMap"
                  android:label="@string/app_name"
                  android:configChanges="keyboardHidden|orientation"
                  android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                       android:resource="@xml/searchable"/>
       </activity>
       <uses-library android:name="com.google.android.maps" />
    <meta-data android:value="xxxxxxxxxxx" android:name="ADMOB_PUBLISHER_ID"/>
    <meta-data android:value="true" android:name="ADMOB_ALLOW_LOCATION_FOR_ADS"/>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="8"/>
    </manifest> 
    

    活动

    private void doMain() {
        mapView = (MapView) findViewById(R.id.mapview);
        MapView localMapView = mapView;
    
        localMapView.setBuiltInZoomControls(true);
        final MapController controller = localMapView.getController();
    
        localMapView.getZoomButtonsController().setAutoDismissed(false);
    
        lastSearchButton = (ImageButton) findViewById(R.id.lastSearchButton);
        lastSearchButton.setOnClickListener(new LastSearchButtonListener(this));
    
        GeoPoint point = new GeoPoint((int)(40.8138 * 1e6), (int)(-96.6831 * 1e6));//Lincoln, NE
        controller.animateTo(point);
        controller.setZoom(13);
    
        pushPinItemizedOverlay = getPushPinOverlay();
        localMapView.getOverlays().add(pushPinItemizedOverlay);
    
        new PopulateMapTask(this).execute(); 
    
        locationOverlay = new FixedMyLocationOverlay(this, localMapView);
        localMapView.getOverlays().add(locationOverlay);
    }
    
    @Override
    protected void onPause() {
        locationOverlay.disableMyLocation();
        super.onPause();
    }
    
    @Override
    protected void onResume() {
        locationOverlay.enableMyLocation();
        super.onResume();
    }
    

    2 回复  |  直到 15 年前
        1
  •  1
  •   Nemi    15 年前

    我不肯定,但我想这可能是从admob广告插件我正在使用。如果我启动我的应用程序,然后立即点击主页,gps图标不会消失。但是,如果我让应用程序运行一段时间,然后再返回并点击主页,图标就会消失。可能是admob插件(我 允许获取位置数据)会阻止gps关闭,直到gps超时或什么(我只在没有卫星的地方测试过)。

    编辑1 :更多信息,我发现如果gps图标闪烁,它仍在尝试锁定某个位置。当我退出应用程序时,我的gps指示器不会消失。但是,如果图标是稳定的(表示锁定了某个位置),那么它将在退出应用程序时消失。似乎是超时问题。(我现在没有一个单元来检验admob假设)。

        2
  •  0
  •   CommonsWare    15 年前

    但是,当我按home键或back键进入主屏幕时,GPS将保持运行状态,如带有GPS图标的状态栏顶部所示。

    如果这是在仿真器上,那就是仿真器的问题。一旦GPS状态图标出现,它似乎永远不会消失。