1
我正在创建一个使用谷歌地图的Android应用程序,所以我需要打开GPS,我有一个飞溅活动,即时通讯尝试检查GPS是否打开。问题是我可以检查GPS是否打开,但我不知道如何创建对话框,以便用户打开GPS。我在几个应用程序中看到,当您打开它时,会显示一个对话框,如果GPS应该打开,用户会看到该对话框,如果用户选择是,则GPS会自动打开。如果用户选择否,对话框关闭并再次打开。这个过程重复直到用户选择是。我怎样才能实现这样的事情?如何以编程方式打开GPS
以下是我有这么远:
public class ActividadSplash extends AppCompatActivity {
private static final long SPLASH_SCREEN_DELAY = 6000;
private ProgressBar barra;
private int progressStatus = 0;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
final LocationManager manager = (LocationManager)getApplicationContext().getSystemService (Context.LOCATION_SERVICE);
if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
CheckEnableGPS();
}
else{
barra = (ProgressBar) findViewById(R.id.barraProgreso2);
barra.setScaleY(3f);
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
//Update progress bar with completion of operation
handler.post(new Runnable() {
public void run() {
barra.setProgress(progressStatus);
if(progressStatus==100){
finish();
ParseUser usuario= ParseUser.getCurrentUser();
if(usuario!=null){
Intent intento = new Intent(getApplicationContext(), DrawerPrincipal.class);
intento.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intento.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intento);
}else{
Intent intento = new Intent(getApplicationContext(), ActividadLogin.class);
intento.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intento.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intento);
}
}
// textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
private void CheckEnableGPS() {
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.equals("")){
//GPS Enabled
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enable GPS"); // GPS not found
builder.setMessage("The app needs GPS to be enabled do you want to enable it in the settings? "); // Want to enable?
builder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.setCancelable(false);
builder.create().show();
return;
}
}
}
非常感谢您的帮助队友,工作完全 –
哎交配抱歉打扰你,但我有一个问题,如何控制行为如果用户选择没有 –
它可以静默地打开gps –