我无法在安卓真机上获取位置 [英] I can't get location on Android real phone

查看:37
本文介绍了我无法在安卓真机上获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    public class LocationService extends Service {


        private Handler mHandler = new Handler();
        private Timer mTimer = null;
        private int mCount = 0;

        public static final long NOTIFY_INTERVAL = 10 * 1000;

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            // cancel if already existed
            if (mTimer != null) {
                mTimer.cancel();
            } else {
                // recreate new
                mTimer = new Timer();
            }


            // schedule task
            mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);



        }

        @Override
        public void onDestroy() {
            mTimer.cancel();
        }

        private class TimeDisplayTimerTask extends TimerTask implements LocationListener {

            @Override
            public void run() {

                mHandler.post(new Runnable() {

                    @Override
                    public void run() {

                        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000, 0,
                                TimeDisplayTimerTask.this);

                    }

                });
            }

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                if (location != null) {
                    Toast.makeText(getApplicationContext(), Double.toString(location.getLat(), Toast.LENGTH_LONG).show();
                    Toast.makeText(getApplicationContext(), Double.toString(location.getLng(), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Cant Get Location", Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

        }

此代码在模拟器上运行良好.我可以每 10 秒获得一次位置.但是当我在真机上测试它时,我根本看不到任何东西出现.这段代码有什么问题吗?请告诉我如何解决这个问题.

This code work fine on emulator. I can get location every 10 second. But when I test it on real phone, I don't see anything appear at all. Is it anything wrong with this code? Please show me how to fix this.

推荐答案

你能确认你的服务没有被后台设备杀死吗?

Can you verify that your service is not being killed by the device on background?

(您可以在设置">应用程序">正在运行"选项卡>[您的应用程序名称]那里看到进程计数和服务计数)

(You can see this on Settings > Apps > Running tab > [Your app name] There should be a process count and service count there)

如果在后台杀死服务,Android 设备有自己的方式.如果是这种情况,您应该在前台启动您的服务.防止它被杀死.

Android devices has it's own way if killing services on background. If that's the case, you should start your service on foreground. To prevent it from being killed.

你怎么问?你应该在你的服务开始时添加这个:

How you ask? You should add this on start of your service:

Notification notification = new Notification();
startForeground(1, notification);

你问的通知是什么?当您的应用程序在后台时,这会提示用户,您可以用它替换您的吐司.这是与其相关的链接.检查接受的答案.

What is the notification you ask? This prompts the user when you're app is on background, you can substitute your toast with this. Here's a link related to its this. Check the accepted answer.

Android - 为服务实现 startForeground?

这篇关于我无法在安卓真机上获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆