0

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

}
    }

}
4

1 回答 1

0

公共类 GpsChangeReceiver 扩展 BroadcastReceiver { @Override
public void onReceive( Context context, Intent intent ) { final LocationManager manager = (LocationManager) context.getSystemService( Context.LOCATION_SERVICE ); if (manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) { // Toast.makeText(context, "gps goes on", Toast.LENGTH_LONG).show(); } else { // Toast.makeText(context, "gps 关闭", Toast.LENGTH_LONG).show(); System.exit(0); } } }

在 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");


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


}

}

于 2016-01-04T04:50:53.700 回答