开始解决片段中的结果

Posted

技术标签:

【中文标题】开始解决片段中的结果【英文标题】:Start resolution for result in a fragment 【发布时间】:2017-02-27 21:06:45 【问题描述】:

在我的应用中,如果用户没有打开位置,我会通过一个对话框进行提示,然后尝试通过覆盖活动结果来返回该结果(可能是错误的)。

这是在一个片段中,所以不确定它会如何改变:

这就是我用 startResolutionForResult 将其称为对话框的方式:

public void checkDeviceLocationIsOn()
        
            System.out.println("Test running setting request" );
            LocationRequest locationRequest = LocationRequest.create();
            locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(locationRequest);
            builder.setAlwaysShow(true); //this is the key ingredient

            PendingResult<LocationSettingsResult> 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.
                            System.out.println("Test setting all fine starting location request" );
                            getLocation();
                            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(), LOCATION_SETTINGS_REQUEST_CODE);
                                System.out.println("Test setting not met starting dialog to prompt user" );
                             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) 
        switch (requestCode) 
        // Check for the integer request code originally supplied to startResolutionForResult().
            case LOCATION_SETTINGS_REQUEST_CODE:
                switch (resultCode) 
                    case Activity.RESULT_OK:
                        System.out.println("test user has turned the gps back on");
                        getLocation();
                        break;
                    case Activity.RESULT_CANCELED:

                        System.out.println("test user has denied the gps to be turned on");
                        Toast.makeText(getActivity(), "Location is required to order stations", Toast.LENGTH_SHORT).show();
                        break;
                
                break;
        
    

提前感谢您的帮助

【问题讨论】:

你为什么使用System.out.println()而不是androids Log类? developer.android.com/reference/android/util/Log.html 因为是我自己测试的目的 你有没有得到任何解决方案,我面临同样的问题:( 面临同样的问题。我试图打电话给活动。但是所有在片段上的东西都会变为空。 这里已经回答了这个问题:***.com/a/39579124/4514796 【参考方案1】:

调用片段方法

startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CODE_LOCATION_SETTING, null, 0, 0, 0, null);

而不是

status.startResolutionForResult(...)

【讨论】:

没有什么比谷歌的 api 更稳定了。岁月流逝,一切仍然是真实的xD【参考方案2】:

要开始解析片段中的结果,您应该使用新的 API

首先,创建 ActivityResultLanucher

private val resolutionForResult =
    registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult())  activityResult ->
        if (activityResult.resultCode == RESULT_OK)
            //startLocationUpdates() or do whatever you want
        else 
            showMessage("we can't determine your location")
        
    

然后像这样使用它

val intentSenderRequest = IntentSenderRequest.Builder(exception.resolution).build()
resolutionForResult.launch(intentSenderRequest)

【讨论】:

【参考方案3】:

也许一个简单的解决方案是使用两个活动,并通过使用清单上的任何对话框主题来显示一个像对话框一样

<activity
        android:name=".LocationDialogActivity"
        android:theme="@style/DialogStyle"></activity>

他们只是像你一直使用的那样使用 onActivityResult。

对话活动

 private void setLocation() 
    Intent intentResult = new Intent();
    intentResult.putExtra("latitude", "valueLat");
    intentResult.putExtra("longitude", "valueLon");
    setResult(RESULT_OK, intentResult);
    finish();

主活动

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    //super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) 
            /*Take your data*/
            latitude = data.getExtras().getString("latitude");
            longitude = data.getExtras().getString("longitude");
        

【讨论】:

【参考方案4】:

在超级活动中执行这个方法...

在 MainActivity 中

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)

    final int REQUEST_CHECK_SETTINGS = 100;//check GPS setting
    Log.d("onActivityResult()", Integer.toString(resultCode));

    //final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
    switch (requestCode)
    
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode)
            
                case Activity.RESULT_OK:
                
                    // All required changes were successfully made
                    Toast.makeText(this, "Location enabled by user!", Toast.LENGTH_LONG).show();
                    ContactFragment.isGPSEnabled=true;
                    ContactFragment.click();
                    break;
                
                case Activity.RESULT_CANCELED:
                
                    // The user was asked to change settings, but chose not to
                    Toast.makeText(this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show();
                    ContactFragment.isGPSEnabled=false;
                    break;
                
                default:
                
                    break;
                
            
            break;
    


ContactFragment 内部

@Override
public void onConnected(@Nullable 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(googleApiClient, builder.build());
    result.setResultCallback(this);

  @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_CHECK_SETTINGS);
             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;
    

【讨论】:

以上是关于开始解决片段中的结果的主要内容,如果未能解决你的问题,请参考以下文章

启动对话框以获取结果以将值返回给主要活动

未解决的对“片段”部分中的符号“”的引用

EF添加关联的提示问题:映射从第 260 行开始的片段时有问题:

在多个片段中的片段中实现选项卡

Chrome-Devtools代码片段中的多个JS库

片段:java.lang.RuntimeException:传递结果ResultInfo失败