2015-12-27 77 views
0

有一個應用程序,它會工作,而電話的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; 

} 
    } 

} 
+0

你可以什麼行不通添加更多的細節 - 我的意思是準確地知道你得到了什麼錯誤? – prasun

+0

當用戶從程序關閉GPS,退出或強制關閉它 – naim

回答

0

公共類GpsChangeReceiver延伸的BroadcastReceiver {
@Override 公共無效的onReceive(上下文範圍內,意圖意圖) { 最終的LocationManager經理=(的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 goes 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)); 


} 

}

+0

和添加此代碼到的AndroidManifest.xml: <接收機機器人:名稱= 「GpsChangeReceiver」> \t \t \t <意圖濾波器> \t \t \t \t <操作機器人:名稱= 「android.location.PROVIDERS_CHANGED」/> \t \t \t \t \t – naim