如何避免 GPS 状态问题 - 在我的 Android 应用程序中暂时不可用

发布于 2024-11-05 09:27:20 字数 2757 浏览 0 评论 0原文

我正在编写一个Android应用程序来获取我的设备的位置,它总是显示GPS状态为暂时不可用,即使我正在更改我的手机的位置,它甚至没有调用位置更改方法,您能调查一下这个问题吗?下面的代码

    ls= (ArrayList<String>)lm.getProviders(false);

    Log.e(""," SIZE "+ls.size());
    sb=new StringBuilder();
    for(int i=0;i<ls.size();i++)
        {Log.e("","Data : "+ls.get(i));
        lp=lm.getProvider(ls.get(i));

        sb.append("\nProvider Name : "+lp.getName());
        sb.append("\nPower Requirement :"+lp.getPowerRequirement());
        sb.append("\nAccuracy : "+lp.getAccuracy()+"\n\n");

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this);

        Location location=lm.getLastKnownLocation(ls.get(i));           
        }

    strt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            TextView tv=(TextView)findViewById(R.id.TextView01);
            tv.setText("StarteD :"+sb);

        }
    });

    stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            TextView tv=(TextView)findViewById(R.id.TextView01);
            tv.setText("STOPPED ys  :"+res);
            Location location=lm.getLastKnownLocation(ls.get(1));

            if(location==null)
                Log.e("","location object null");
            try{
            tv.append("\nLogitude: "+location.getLongitude());
            tv.append("\nLattitude: "+location.getLatitude());
            tv.append("\nTime:"+location.getTime());
            tv.append("\nSpeed: "+location.getSpeed());
            tv.append("\n Provider:"+location.getProvider());

            }catch(Exception e){}
        }
    });


}
@Override
public void onLocationChanged(Location location) {
    Log.e("","LocationChandgeD... ");
}
@Override
public void onProviderDisabled(String provider) {
    Log.e("",provider+" DIsabled... ");
}
@Override
public void onProviderEnabled(String provider) {
    Log.e("",provider+" Enabled... ");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    switch (status) {
    case LocationProvider.OUT_OF_SERVICE:
        Toast.makeText(this, "Status Changed: Out of Service",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.TEMPORARILY_UNAVAILABLE:
        Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.AVAILABLE:
        Log.e("", "Status Changed: Available");         
        Toast.makeText(this, "Status Changed: Available",
                Toast.LENGTH_SHORT).show();
        break;
    }

}

I am writing an android application to get location of my device, It is always showing GPS status as TEMPORARILY UN AVAILABLE, its even not calling Location Changed method even though i am changing location of my mobile, could you please look into the issue with the below code

    ls= (ArrayList<String>)lm.getProviders(false);

    Log.e(""," SIZE "+ls.size());
    sb=new StringBuilder();
    for(int i=0;i<ls.size();i++)
        {Log.e("","Data : "+ls.get(i));
        lp=lm.getProvider(ls.get(i));

        sb.append("\nProvider Name : "+lp.getName());
        sb.append("\nPower Requirement :"+lp.getPowerRequirement());
        sb.append("\nAccuracy : "+lp.getAccuracy()+"\n\n");

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this);

        Location location=lm.getLastKnownLocation(ls.get(i));           
        }

    strt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            TextView tv=(TextView)findViewById(R.id.TextView01);
            tv.setText("StarteD :"+sb);

        }
    });

    stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            TextView tv=(TextView)findViewById(R.id.TextView01);
            tv.setText("STOPPED ys  :"+res);
            Location location=lm.getLastKnownLocation(ls.get(1));

            if(location==null)
                Log.e("","location object null");
            try{
            tv.append("\nLogitude: "+location.getLongitude());
            tv.append("\nLattitude: "+location.getLatitude());
            tv.append("\nTime:"+location.getTime());
            tv.append("\nSpeed: "+location.getSpeed());
            tv.append("\n Provider:"+location.getProvider());

            }catch(Exception e){}
        }
    });


}
@Override
public void onLocationChanged(Location location) {
    Log.e("","LocationChandgeD... ");
}
@Override
public void onProviderDisabled(String provider) {
    Log.e("",provider+" DIsabled... ");
}
@Override
public void onProviderEnabled(String provider) {
    Log.e("",provider+" Enabled... ");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    switch (status) {
    case LocationProvider.OUT_OF_SERVICE:
        Toast.makeText(this, "Status Changed: Out of Service",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.TEMPORARILY_UNAVAILABLE:
        Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.AVAILABLE:
        Log.e("", "Status Changed: Available");         
        Toast.makeText(this, "Status Changed: Available",
                Toast.LENGTH_SHORT).show();
        break;
    }

}

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

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

发布评论

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

评论(1

输什么也不输骨气 2024-11-12 09:27:20

不幸的是,如果没有清晰的天空视野(室内或茂密的针叶树覆盖范围),GPS 就无法工作。也许你应该尝试到户外去测试你的程序。

Unfortunaitly GPS doesn't work without a clear view of the sky (indoors or around dense coniferous tree coverage). Maybe you should try moving outdoors to test your program.

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