每次在android中关闭或关闭gps

时间:2015-12-27 07:33:41

标签: android-gps

有一个应用程序,当手机的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;

}
    }

}

1 个答案:

答案 0 :(得分:0)

公共类GpsChangeReceiver扩展了BroadcastReceiver {
  @覆盖   public void onReceive(Context context,Intent intent)   {       final LocationManager manager =(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);           if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ // Toast.makeText(上下文,“gps继续”,Toast.LENGTH_LONG)。show();             }          其他          { // Toast.makeText(context,“gps off off”,Toast.LENGTH_LONG)。show();             System.exit(0);          }   } }

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

@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));


}

}