ऐसा लगता है कि मुख्य मुद्दा यह है कि आपके पास फ्रैगमेंट में सभी कोड हैं, और startResolutionForResult()
में उस गतिविधि को पारित करने की आवश्यकता है, गतिविधि onActivityResult()
कॉलबैक प्राप्त करती है।
एक तरीका यह है कि चारों ओर पाने के लिए, तकनीक here वर्णित उपयोग करने के लिए मैन्युअल रूप से गतिविधि से टुकड़ा के onActivityResult()
विधि कॉल जब परिणाम में आता है।
मैं सिर्फ इस सरल काम कर उदाहरण मिला है।
पहले, गतिविधि, जो टुकड़ा कहते हैं, और यह भी टुकड़ा करने के लिए onActivityResult()
का नतीजा साथ दी जाने वाली कार्यक्षमता है:
public class MainActivity extends AppCompatActivity{
LocationFragment lFrag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lFrag = LocationFragment.newInstance();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, lFrag).commit();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == LocationFragment.REQUEST_LOCATION){
lFrag.onActivityResult(requestCode, resultCode, data);
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
यहाँ टुकड़ा है, जो कार्यक्षमता के सभी दिखाने के लिए होता है संवाद, और परिणाम संभाल लें। इस सरल उदाहरण में मैंने टोस्ट संदेशों का उपयोग यह सत्यापित करने के लिए किया कि यह अपेक्षित काम कर रहा है। ध्यान दें कि आपके प्रश्न में कोड से मैंने जो मुख्य परिवर्तन किया है वह getActivity()
का उपयोग startResolutionForResult()
पर कॉल के लिए आवश्यक गतिविधि संदर्भ प्राप्त करने के लिए है।
public class LocationFragment extends Fragment
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
PendingResult<LocationSettingsResult> result;
final static int REQUEST_LOCATION = 199;
public static LocationFragment newInstance() {
LocationFragment fragment = new LocationFragment();
return fragment;
}
public LocationFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mGoogleApiClient.connect();
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_location, container, false);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(30 * 1000);
mLocationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);
result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 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:
// All location settings are satisfied. The client can initialize location
// requests here.
//...
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
getActivity(),
REQUEST_LOCATION);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
//...
break;
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("onActivityResult()", Integer.toString(resultCode));
//final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode)
{
case REQUEST_LOCATION:
switch (resultCode)
{
case Activity.RESULT_OK:
{
// All required changes were successfully made
Toast.makeText(getActivity(), "Location enabled by user!", Toast.LENGTH_LONG).show();
break;
}
case Activity.RESULT_CANCELED:
{
// The user was asked to change settings, but chose not to
Toast.makeText(getActivity(), "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show();
break;
}
default:
{
break;
}
}
break;
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
यहाँ परिणाम नेत्रहीन हैं, पहला संवाद नहीं दिखाया जाता है, तो स्थान मोड अक्षम किया गया है:
फिर, यदि उपयोगकर्ता कोई क्लिक करता है तो परिणाम की गतिविधि से करने के लिए पारित हो जाता है टुकड़ा है, जो एक टोस्ट पता चलता है: जब उपयोगकर्ता हाँ क्लिक करता है
यही बात, ख एक सफलता परिणाम के साथ केन्द्र शासित प्रदेशों, और स्थान मोड सक्षम है:
ध्यान दें कि यह सिर्फ गतिविधि में इस कार्यक्षमता के सभी रखने के लिए एक बेहतर विकल्प हो सकता है, और फिर टुकड़ा में एक सार्वजनिक विधि में फोन जब परिणाम आता है।
गतिविधि में कार्यक्षमता को बनाए रखने के लिए यहां पूरी तरह से काम करने वाला कोड है। बेशक इस समाधान में, आपको onActivityResult()
के बाद स्थान मोड की स्थिति अपडेट करने के लिए फ्रैगमेंट में कॉल जोड़ने की आवश्यकता होगी।
public class MainActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
PendingResult<LocationSettingsResult> result;
final static int REQUEST_LOCATION = 199;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(30 * 1000);
mLocationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);
result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 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:
// All location settings are satisfied. The client can initialize location
// requests here.
//...
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
MainActivity.this,
REQUEST_LOCATION);
} catch (SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
//...
break;
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("onActivityResult()", Integer.toString(resultCode));
//final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode)
{
case REQUEST_LOCATION:
switch (resultCode)
{
case Activity.RESULT_OK:
{
// All required changes were successfully made
Toast.makeText(MainActivity.this, "Location enabled by user!", Toast.LENGTH_LONG).show();
break;
}
case Activity.RESULT_CANCELED:
{
// The user was asked to change settings, but chose not to
Toast.makeText(MainActivity.this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show();
break;
}
default:
{
break;
}
}
break;
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
ActivityResult –
पर स्थान को रीफ्रेश कैसे करें उपयोगकर्ता ने स्थान सेवाओं को सक्षम करने के बाद आपको स्थान अपडेट का अनुरोध करने की आवश्यकता होगी। यहां देखें: http://stackoverflow.com/questions/34582370/how-can-i-use-google-maps-and-locationmanager-to-show-current-location-on-androi –
हाय @DanielNugent पोस्ट के लिए धन्यवाद । मेरे पास एक ऐसी स्थिति है जहां मैं जांच कर रहा हूं कि प्रत्येक बार जब मैं बटन पर क्लिक करता हूं तो उपयोगकर्ता की सेटिंग्स की अपेक्षा की जाती है। जिसका अर्थ है, मैं यह देखने के लिए हर बार परिणाम.setResultCallback बुला रहा हूं कि उपयोगकर्ता ने सेटिंग बदल दी है या नहीं। आश्चर्यजनक रूप से, यह केवल पहली बार काम करता है। onResult कभी नहीं बुलाया जाएगा। मेरे पास एक ही सवाल है http://stackoverflow.com/questions/38151650/android-fusedlocations-settingsapi-does-not-work-more-than-once। क्या आप कृपया –