有一個應用程序,它會工作,而電話的GPS是在上,否則它不應該工作。檢查GPS關閉或每次在Android
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;
}
}
}
你可以什麼行不通添加更多的細節 - 我的意思是準確地知道你得到了什麼錯誤? – prasun
當用戶從程序關閉GPS,退出或強制關閉它 – naim