2016-10-07 291 views
0

如何允许用户在启动屏幕本身上启用该位置?如何在禁用用户屏幕时启动启动屏幕

我有代码中有一个对话框,以启用用户的位置在应用程序的开始。

当应用程序首次启动时,启动画面几秒钟可见,如果用户的位置被禁用,则会弹出一个让用户启用位置但当时没有背景的对话框。

如果用户的位置被禁用,我想同时加载对话框和启动画面(闪屏在后台)。

任何想法?

String status = null; 
Bundle bundle = null; 
StartLocationAlert startLocationAlert; 
protected static final int REQUEST_CHECK_SETTINGS = 1; 
GoogleApiClient googleApiClient; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash_screen); 
    googleApiClient = getInstance(); 
    bundle = getIntent().getExtras(); 
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Boolean b1= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    LocationManager locationManager1 = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Boolean b2=locationManager1.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 


    if (!b1&&!b2) { 

     final Thread thread = new Thread() { 
      @Override 
      public void run() { 
       super.run(); 
       try { 
        sleep(2000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } finally { 

        // Boolean b3=startLocationAlert.settingsrequest(); 

        Log.e("settingsrequest","Comes"); 
        LocationRequest locationRequest = LocationRequest.create(); 
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
        locationRequest.setInterval(30 * 1000); 
        locationRequest.setFastestInterval(5 * 1000); 
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
          .addLocationRequest(locationRequest); 
        builder.setAlwaysShow(true); //this is the key ingredient 

        PendingResult<LocationSettingsResult> result = 
          LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); 
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
         @Override 
         public void onResult(LocationSettingsResult result) { 
          final Status status = result.getStatus(); 
          final LocationSettingsStates state = result.getLocationSettingsStates(); 
          switch (status.getStatusCode()) { 
           case LocationSettingsStatusCodes.SUCCESS: 

            break; 
           case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 

            try { 
             status.startResolutionForResult(SplashScreen.this, REQUEST_CHECK_SETTINGS); 
            } catch (IntentSender.SendIntentException e) { 
             Log.e("Applicationsett",e.toString()); 
            } 

            break; 
           case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
            Toast.makeText(SplashScreen.this, "Location is Enabled", Toast.LENGTH_SHORT).show(); 

            break; 
          } 
         } 
        }); 
        finish(); 
       } 
      } 
     }; 

     // startLocationAlert = new StartLocationAlert(this); 
     thread.start(); 

    } else { 
     startActivityThread(); 
    } 
} 
public void startActivityThread(){ 
    final Thread thread = new Thread() { 
     @Override 
     public void run() { 
      super.run(); 
      try { 
       sleep(2000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } finally { 
       Intent intent = null; 
       boolean loggedIn = false; 
       SharedPreferences w = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
       SharedPreferences.Editor share = w.edit(); 
       loggedIn = (w.getBoolean("loggedIn", loggedIn)); 
       System.out.println("logged in " + loggedIn); 
       if (loggedIn) { 
        intent = new Intent(SplashScreen.this, TestNavigationFragments.class); 
       } else { 
        intent = new Intent(SplashScreen.this, Login.class); 
       } 
       startActivity(intent); 
       finish(); 
      } 
     } 
    }; 
    thread.start(); 
} 



@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
     case 1: 
      switch (resultCode) { 
       case Activity.RESULT_OK: 
        Intent intent = null; 
        boolean loggedIn = false; 
        SharedPreferences w = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
        SharedPreferences.Editor share = w.edit(); 
        loggedIn = (w.getBoolean("loggedIn", loggedIn)); 
        System.out.println("logged in " + loggedIn); 
        if (loggedIn) { 
         intent = new Intent(SplashScreen.this, TestNavigationFragments.class); 
        } else { 
         intent = new Intent(SplashScreen.this, Login.class); 
        } 
        startActivity(intent); 
        finish(); 
        break; 
       case Activity.RESULT_CANCELED: 
        break; 
       default: 
        break; 
      } 
      break; 
    } 
} 


@Override 
public void onConnected(@Nullable Bundle bundle) { 

} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 
public GoogleApiClient getInstance(){ 
    GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build(); 
    return mGoogleApiClient; 
} 
+1

您可以发表一些您的代码,以便我们可以帮助您吗? – RCaetano

+0

@RCaetano请找到更新的帖子。 – chandra

+0

任何人都可以请帮我解决这个问题吗? – chandra

回答

0

问题是使用startresolutionForResult()该方法的方法。结果是由处理onActivityResult()方法解决。

在OnActivityResult()方法中处理对话框的结果,并且只在点击是时,闪屏将会休眠,否则它将在对话框的背景中。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 1) { 
     if (resultCode == -1) { 
      final Thread thread = new Thread() { 
       @Override 
       public void run() { 
        super.run(); 
        try { 
         sleep(2000); 
         finish(); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } finally { 
         Intent intent = null; 
         boolean loggedIn = false; 
         SharedPreferences w = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
         SharedPreferences.Editor share = w.edit(); 
         loggedIn = (w.getBoolean("loggedIn", loggedIn)); 
         System.out.println("logged in " + loggedIn); 
         if (loggedIn) { 
          intent = new Intent(SplashScreen.this, TestNavigationFragments.class); 
         } else { 
          intent = new Intent(SplashScreen.this, Login.class); 
         } 
         startActivity(intent); 

        } 
       } 
      }; 
      thread.start(); 
     } else { 
      finish(); 
     } 
    } 
}