2015-07-09 2 views
9

here आधिकारिक मार्गदर्शिका द्वारा प्रदान किया गया कोड है, जबकि यह एक स्निपेट समस्या पैदा कर रहा है।एंड्रॉइड: Google API कनेक्शन को हल करने के लिए कैसे सेवा से विफल हो?

@Override 
public void onConnectionFailed(ConnectionResult result) { 
    if (mResolvingError) { 
     // Already attempting to resolve an error. 
     return; 
    } else if (result.hasResolution()) { 
     try { 
      mResolvingError = true; 
      result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR); 
     } catch (IntentSender.SendIntentException e) { 
      // There was an error with the resolution intent. Try again. 
      mGoogleApiClient.connect(); 
     } 
    } else { 
     // Show dialog using GooglePlayServicesUtil.getErrorDialog() 
     mResolvingError = true; 
     GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, REQUEST_RESOLVE_ERROR) 
       .setOnDismissListener(new DialogInterface.OnDismissListener() { 
        @Override 
        public void onDismiss(DialogInterface dialog) { 
         mResolvingError = false; 
        } 
       }); 
    } 
} 

अगर मैं एक सेवा, में इसका इस्तेमाल करते हैं जब आप चर this उन कार्यों के लिए तर्क के रूप में पारित कर दिया पढ़ते हैं, वे एक गतिविधि प्रकार की उम्मीद है। मुझे कैसे करना चाहिए? यह एक सेवा है।

इसी कारण मैं गतिविधि परिणाम

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (requestCode == REQUEST_RESOLVE_ERROR) { 
    mResolvingError = false; 
    if (resultCode == RESULT_OK) { 
     // Make sure the app is not already connected or attempting to connect 
     if (!mGoogleApiClient.isConnecting() && 
       !mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.connect(); 
     } 
    } 
} 
} 
+0

इसलिए आपकी समस्या किसी गतिविधि का संदर्भ प्राप्त कर रही है ?? सेवा के जीवनकाल में गतिविधियों भी जीवित है? – Elltz

+0

किस प्रकार की सेवा: शुरू, बाध्य, इरादा? –

+0

तब शुरू हुआ जब गतिविधि किसी भी राज्य में हो सकती है – user3290180

उत्तर

6

इस उत्तर मान लिया गया है आपकी सेवा के लिए एक "शुरू कर दिया" सेवा है नहीं मिल सकता है। यदि यह एक बाध्य सेवा या मंशा सेवा है, तो संकेत दें कि एक टिप्पणी में और मैं यहां शामिल विवरण और कोड अपडेट करूंगा।

मेरा सुझाव है कि समाधान समाधान यूआई को संभालने के लिए नीचे दिखाए गए गतिविधि को कार्यान्वित करना है। ResolverActivity को संकल्प प्रसंस्करण बंद हाथ के लिए इस कोड के साथ अपनी सेवा में onConnectionFailed() विधि बदलें:

@Override 
public void onConnectionFailed(ConnectionResult result) { 
    Intent i = new Intent(this, ResolverActivity.class); 
    i.putExtra(ResolverActivity.CONNECT_RESULT_KEY, result); 
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(i); 
} 

गतिविधि को अपने ऐप्लिकेशन पर नीचे दिखाया गया है जोड़ें। जब आपकी सेवा में कनेक्शन अनुरोध विफल हो जाता है, तो कनेक्शन परिणाम, जो Parcelable है, गतिविधि को पास कर दिया जाता है। गतिविधि रिज़ॉल्यूशन यूआई को संभालने और समाप्त होने पर, सेवा को स्थिति को अतिरिक्त इरादे के रूप में वापस कर देती है। आपको यह निर्धारित करने के इरादे में एक्स्ट्रा की जांच करने के लिए अपनी सेवा के onStartCommand() में कोड को संशोधित करने की आवश्यकता होगी या नहीं, इसे पहली बार सेवा शुरू करने के लिए कहा जा रहा है या ResolverActivity से रिज़ॉल्यूशन स्थिति प्राप्त करने के लिए।

इस दृष्टिकोण में वृद्धि PendingIntent के साथ ResolverActivity के लिए तुरंत अधिसूचना लॉन्च करने के बजाय अधिसूचना पोस्ट करना होगा। इससे उपयोगकर्ता को कनेक्शन विफलता के समाधान को हल करने का विकल्प मिल जाएगा।

public class ResolverActivity extends AppCompatActivity { 
    public static final String TAG = "ResolverActivity"; 

    public static final String CONNECT_RESULT_KEY = "connectResult"; 

    public static final String CONN_STATUS_KEY = "connectionStatus"; 
    public static final int CONN_SUCCESS = 1; 
    public static final int CONN_FAILED = 2; 
    public static final int CONN_CANCELLED = 3; 

    // Request code to use when launching the resolution activity 
    private static final int REQUEST_RESOLVE_ERROR = 1111; 

    private static final String ERROR_CODE_KEY = "errorCode"; 
    private static final String DIALOG_FRAG_TAG = "errorDialog"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Log.i(TAG, "onCreate()"); 

     // No content needed. 
     //setContentView(R.layout.activity_main); 

     Intent i = getIntent(); 

     ConnectionResult result = i.getParcelableExtra(CONNECT_RESULT_KEY); 

     if (result.hasResolution()) { 
      try { 
       Log.i(TAG, "Starting error resolution..."); 
       result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR); 
      } catch (IntentSender.SendIntentException e) { 
       // There was an error with the resolution intent. 
       sendStatusToService(CONN_FAILED); 
       finish(); 
      } 
     } else { 
      // Show dialog using GooglePlayServicesUtil.getErrorDialog() 
      ErrorDialogFragment.newInstance(result.getErrorCode()) 
        .show(getSupportFragmentManager(), DIALOG_FRAG_TAG); 
     } 
    } 

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

     if (requestCode == REQUEST_RESOLVE_ERROR) { 
      if (resultCode == RESULT_OK) { 
       Log.i(TAG, "onActivityResult(): Connection problem resolved"); 
       sendStatusToService(CONN_SUCCESS); 
      } else { 
       sendStatusToService(CONN_CANCELLED); 
       Log.w(TAG, "onActivityResult(): Resolution cancelled"); 
      } 
      // Nothing more to do in this activity 
      finish(); 
     } 
    } 

    private void sendStatusToService(int status) { 
     Intent i = new Intent(this, MyGoogleApiService.class); 
     i.putExtra(CONN_STATUS_KEY, status); 
     startService(i); 
    } 

    // Fragment to display an error dialog 
    public static class ErrorDialogFragment extends DialogFragment { 

     public static ErrorDialogFragment newInstance(int errorCode) { 
      ErrorDialogFragment f = new ErrorDialogFragment(); 
      // Pass the error that should be displayed 
      Bundle args = new Bundle(); 
      args.putInt(ERROR_CODE_KEY, errorCode); 
      f.setArguments(args); 
      return f; 
     } 

     @Override 
     @NonNull 
     public Dialog onCreateDialog(Bundle savedInstanceState) { 
      // Get the error code and retrieve the appropriate dialog 
      int errorCode = getArguments().getInt(ERROR_CODE_KEY); 
      return GooglePlayServicesUtil.getErrorDialog(
        errorCode, getActivity(), REQUEST_RESOLVE_ERROR); 
     } 

     @Override 
     public void onDismiss(DialogInterface dialog) { 
      Log.i(TAG, "Dialog dismissed"); 
     } 
    } 
} 
+0

यह काम करता है लेकिन मैंने mesesolving बूलियन जोड़ा क्योंकि मार्गदर्शिका पिछले कुछ को हल करते समय असफलताओं की पुनरावृत्ति से बचने के लिए कहती है। – user3290180

+0

संवाद पर खारिज कर दिया गया है – user3290180

+0

पर सक्रियता परिणाम हमेशा गलत अनुरोध वापस करें मेरे मामले में कोड क्या आप इस संबंध में मदद कर सकते हैं – ingsaurabh

संबंधित मुद्दे