Android 服务未接收位置更新

发布于 2024-11-19 16:10:46 字数 3563 浏览 1 评论 0原文

然而,尝试在我的服务中获取 LocationUpdates 时,它似乎从未触发 LocationListener。

我的服务启动一个线程,然后使用 Handler.postDelayed() 循环 - 我最初使用 sleep(30000),但我认为这可能会阻止 locationUpdates。该服务所做的就是启动该线程 - 并且 onStartCommand 返回 STICKY 以保持其运行:

public class TST extends Thread {

    final int DefaultNetworkTick = 120000; // 2 minutes
    final int DefaultLocationTick = 20000; //600000;  
    boolean CustomLocationUpdateTick = false; // If the network requests faster updates
    int CustomLocationUpdateTickMs = 600000; // Default 10 minutes

    public boolean kill = false;
    public Context context;

    Handler handler  = new Handler();

    Date nextNetworkReadTick;

    @Override
    public void run() {        
        Log.i("TST", "Start run() iteration");
        // Set the ticks to the appropriate values
        // Network gets read every 2 minutes

        nextNetworkReadTick = new Date();
        nextNetworkReadTick.setTime(nextNetworkReadTick.getTime() + DefaultNetworkTick);
        Log.i("TST", "Set next Network tick to " + nextNetworkReadTick.toString());
        Looper.prepare();
        Log.i("TST", "Performing location update setup...");
        SetupLocationListenerDefault();
        handler.postDelayed(runFunc, 2000);
        Looper.loop();
    }

    // We do our networking and stuff in here
    public Runnable runFunc = new Runnable(){
        public void run()
        {
            Log.i("TST", "run() iteration");

            if( new Date().after(nextNetworkReadTick) ) {
                // Set next tick
                nextNetworkReadTick = new Date();
                nextNetworkReadTick.setTime(nextNetworkReadTick.getTime() + DefaultNetworkTick);

                UpdateNetwork();
            }

            handler.postDelayed(runFunc, 1000);
        }
    };

    private void UpdateNetwork() {
            // TODO Auto-generated method stub
    }

    LocationManager locMgr;

    PendingIntent locationUpdateIntentPending;
    Intent locationUpdateIntent;

    LocationListener locationListener = new LocationListener()
    {
            @Override
            public void onLocationChanged(Location arg0) {
                    // TODO Auto-generated method stub
                    Log.i("TST", "OnLocationChanged: " + arg0.toString());
            }

            @Override
            public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub
                    Log.i("TST", "OnProviderDisabled: " + provider);                
            }

            @Override
            public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub
                    Log.i("TST", "OnProviderEnabled: " + provider);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                    // TODO Auto-generated method stub
                    Log.i("TST", "OnStatusChanged");
            }

    };

    private void SetupLocationListenerDefault() {
        // Reset location manager
        if( locMgr != null ) {
                locMgr.removeUpdates(locationListener);
                locMgr = null;
        }

        locMgr = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = locMgr.getBestProvider(criteria, true);

        locMgr.requestLocationUpdates(provider, DefaultLocationTick, 0, locationListener);
    }

}

Attempting to get LocationUpdates in my Service, however, it doesnt ever seem to fire the LocationListener.

My Service starts a thread, which then loops with a Handler.postDelayed() - I originally used sleep(30000), however I thought this may have been preventing the locationUpdates. All the service does is launch this thread - and onStartCommand returns STICKY to keep it running:

public class TST extends Thread {

    final int DefaultNetworkTick = 120000; // 2 minutes
    final int DefaultLocationTick = 20000; //600000;  
    boolean CustomLocationUpdateTick = false; // If the network requests faster updates
    int CustomLocationUpdateTickMs = 600000; // Default 10 minutes

    public boolean kill = false;
    public Context context;

    Handler handler  = new Handler();

    Date nextNetworkReadTick;

    @Override
    public void run() {        
        Log.i("TST", "Start run() iteration");
        // Set the ticks to the appropriate values
        // Network gets read every 2 minutes

        nextNetworkReadTick = new Date();
        nextNetworkReadTick.setTime(nextNetworkReadTick.getTime() + DefaultNetworkTick);
        Log.i("TST", "Set next Network tick to " + nextNetworkReadTick.toString());
        Looper.prepare();
        Log.i("TST", "Performing location update setup...");
        SetupLocationListenerDefault();
        handler.postDelayed(runFunc, 2000);
        Looper.loop();
    }

    // We do our networking and stuff in here
    public Runnable runFunc = new Runnable(){
        public void run()
        {
            Log.i("TST", "run() iteration");

            if( new Date().after(nextNetworkReadTick) ) {
                // Set next tick
                nextNetworkReadTick = new Date();
                nextNetworkReadTick.setTime(nextNetworkReadTick.getTime() + DefaultNetworkTick);

                UpdateNetwork();
            }

            handler.postDelayed(runFunc, 1000);
        }
    };

    private void UpdateNetwork() {
            // TODO Auto-generated method stub
    }

    LocationManager locMgr;

    PendingIntent locationUpdateIntentPending;
    Intent locationUpdateIntent;

    LocationListener locationListener = new LocationListener()
    {
            @Override
            public void onLocationChanged(Location arg0) {
                    // TODO Auto-generated method stub
                    Log.i("TST", "OnLocationChanged: " + arg0.toString());
            }

            @Override
            public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub
                    Log.i("TST", "OnProviderDisabled: " + provider);                
            }

            @Override
            public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub
                    Log.i("TST", "OnProviderEnabled: " + provider);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                    // TODO Auto-generated method stub
                    Log.i("TST", "OnStatusChanged");
            }

    };

    private void SetupLocationListenerDefault() {
        // Reset location manager
        if( locMgr != null ) {
                locMgr.removeUpdates(locationListener);
                locMgr = null;
        }

        locMgr = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = locMgr.getBestProvider(criteria, true);

        locMgr.requestLocationUpdates(provider, DefaultLocationTick, 0, locationListener);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悍妇囚夫 2024-11-26 16:10:46

重新启动我的手机并开始获取更新 - 似乎 Android 被卡在某个地方了!

Rebooted my phone and started getting updates - seems that Android had got stuck somewhere!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文