【问题标题】:check gps off or on every time in android每次在android中检查gps关闭或打开
【发布时间】:2015-12-27 07:33:40
【问题描述】:

有一个应用程序,它可以在手机的 gps 开启时运行,否则它应该无法运行。

public class Gps extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gps);
    TextView txtgps = (TextView) findViewById(R.id.textView1);
txtgps.setText("if you can see this page, it means your\n phone's gps is on");
}


}

public class MainActivity extends Activity implements LocationListener{

public static boolean isgpsenabled = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(isgpsenabled){
        Toast.makeText(getApplicationContext(), "You can work with program", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(getApplicationContext(), "You cant work until you turn on the gps", Toast.LENGTH_SHORT).show();
    }

    Button btngps = (Button) findViewById(R.id.button1);
    btngps.setOnClickListener(new OnClickListener() {
        //LocationListener locationListener = new MyLocationListener(MainActivity.this);

        @Override
        public void onClick(View v) {

            LocationManager service = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            LocationListener locationListener = new MyLocationListener(MainActivity.this);
            service.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 5000, 10, locationListener);

            boolean enabled = service
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
            if (!enabled) {
                Toast.makeText(getBaseContext(), "gps دستگاه را روشن کرده و دوباره تلاش کنید.", Toast.LENGTH_SHORT).show();
            }else

            startActivity(new Intent(MainActivity.this, Gps.class));


        }
    });

    }







@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}


@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

}
public void onProviderDisabled(String provider) {

    if(LocationManager.GPS_PROVIDER.equals(provider)){

    Toast.makeText(getBaseContext(), "zzzzzzzzzzzz", Toast.LENGTH_SHORT).show();

}}
}

public class MyLocationListener implements LocationListener {
Context ctx;

public MyLocationListener(MainActivity c){
    ctx = c.getApplicationContext();
}
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@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
    MainActivity.isgpsenabled=true;
}

@Override
public void onProviderDisabled(String provider) {

    if(LocationManager.GPS_PROVIDER.equals(provider)){

    Toast.makeText(ctx, "gps turned off", Toast.LENGTH_SHORT).show();
    MainActivity.isgpsenabled=false;

}
    }

}

【问题讨论】:

  • 您能否添加更多关于什么不起作用的详细信息 - 我的意思是您遇到了什么错误?
  • 当用户关闭gps、退出程序或强制关闭时

标签: android-gps


【解决方案1】:

公共类 GpsChangeReceiver 扩展 BroadcastReceiver {
@Override public void onReceive(上下文上下文,意图意图) { final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 如果(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ // Toast.makeText(context, "gps 继续", Toast.LENGTH_LONG).show(); } 别的 { // Toast.makeText(context, "gps 关闭", Toast.LENGTH_LONG).show(); System.exit(0); } } }

在 gps 类中,您应该添加以下代码 公共类 Gps 扩展 Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gps);
    TextView txtgps = (TextView) findViewById(R.id.textView1);
txtgps.setText("if you can see this page, it means your\n phone's gps is on");


GpsChangeReceiver m_gpsChangeReceiver = new GpsChangeReceiver();
this.registerReceiver(m_gpsChangeReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));


}

}

【讨论】:

  • 并将此代码添加到 androidmanifest.xml: 意图过滤器>
猜你喜欢
  • 2015-05-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多