我不能在Android上真正的手机获得位置 [英] I can't get location on Android real phone

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

问题描述

    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

            }

        }

这code工作在模拟器的罚款。我能得到的位置,每10秒。但是,当我测试它真实的电话,我没有看到任何东西出现在所有。它是什么毛病此code?请告诉我如何解决这个问题。

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设备如果有背景查杀服务它自己的方式。如果是那样的话,你应该开始您的前台服务。若要从被杀害prevent它。

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.

<一个href=\"http://stackoverflow.com/questions/6397754/android-implementing-startforeground-for-a-service\">Android - 实施startForeground为服务

这篇关于我不能在Android上真正的手机获得位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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