android - 如何在手机追踪器应用程序中实现简单的 "No Connection Toast ..."

标签 android gps location tracking

下面我展示了我当前运行的代码,目前简单明了。

然而,我想实现一个在没有 GPS 连接时显示的 toast。

这是我项目的下一步,所以,没有它我真的无法继续前进。

我遵循了一些教程,但对于一个简单的 toast 来说,它们是徒劳的。

谁能给我一个简单的例子来解决这个小问题?

public class MainActivity extends Activity
{
    TextView TxtLat;
    TextView TxtLong;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TxtLat = (TextView) findViewById(R.id.TxtLat);
        TxtLong = (TextView) findViewById(R.id.TxtLong);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new myLocationListener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    }
        class myLocationListener implements LocationListener
        {

            @Override
            public void onLocationChanged(Location location)
            {
                if (location != null)
                {
                    double pLong = location.getLongitude();
                    double pLat = location.getLatitude();

                    TxtLat.setText(Double.toString(pLat));
                    TxtLong.setText(Double.toString(pLong));
                }
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        }

    }

最佳答案

这样实现:

    LocationManager locationManager = (LocationManager) youractivity.this.getSystemService(LOCATION_SERVICE);

  if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

        Toast toast = Toast.makeText(youractivity.this, "You can improve the accuracy of your location by turning on: GPS", Toast.LENGTH_SHORT);
        toast.show(); 

  }else if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

        Toast toast = Toast.makeText(youractivity.this, "You can improve the accuracy of your location by turning on: Wi-Fi", Toast.LENGTH_SHORT);
        toast.show(); 
 }else{

        Toast toast = Toast.makeText(youractivity.this, "You can improve the accuracy of your location by turning on: GPS or Wi-Fi", Toast.LENGTH_SHORT);
        toast.show();

    }

并在您的manifest.xml 文件中添加以下权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

关于android - 如何在手机追踪器应用程序中实现简单的 "No Connection Toast ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22557139/

相关文章:

python - Tweepy 位置过滤器不起作用

java - android中的屏幕镜像

android - 在 Android 中正确的纵横比

java - 如何在 Android 中正确且一致地模拟位置?

android - Android 应用程序上的 GPS 位置信息冲突

ios - WKWebView 两次询问位置权限

Android模拟器调试时显示空白

java - 简单的 Android 应用程序崩溃

java - GPS:重新考虑这个答案背后的逻辑

php header 位置与 php_redirect