6

मैं एक सेवा बांधने की कोशिश कर रहा हूं, लेकिन onBind() हमेशा झूठा रिटर्न देता है। है bindService() करने के लिए कॉलऑनबिंड() सेवा पर हमेशा झूठी रिटर्न - एंड्रॉइड

private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     // This is called when the connection with our service has been established, 
     // giving us the service object we can use to interact with our service. 
     mBoundService = ((ScheduleService.ServiceBinder) service).getService(); 
    } 

    public void onServiceDisconnected(ComponentName className) { 
     mBoundService = null; 
    } 
}; 

इस - -

boolean test = getApplicationContext().bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE); 

यह प्रकट में सेवा की घोषणा है -

इस के लिए ServiceConnection कोड है

<service android:name=".Notifications.ScheduleService" android:enabled="true"/> 

मैंने विषय के बारे में पिछले प्रश्न पढ़े हैं और एक उत्तर नहीं मिला wer (गतिविधि संदर्भ के साथ गतिविधि संदर्भ स्विच करने की कोशिश की, लेकिन यह मदद नहीं की)।

मैं Frgaments और ActionBarSherlock का उपयोग कर उपयोग कर रहा हूं, और मेरी गतिविधि SlidingFragmentActivity फैली हुई है (यही कारण है कि मैं एप्लिकेशन संदर्भ का उपयोग कर रहा हूं, जो मदद नहीं करता है)।

संपादित करें -

public class ScheduleService extends Service { 

    /** 
    * Class for clients to access 
    */ 
    public class ServiceBinder extends Binder { 
     public ScheduleService getService() { 
      return ScheduleService.this; 
     } 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.i("ScheduleService", "Received start id " + startId + ": " + intent); 

     // We want this service to continue running until it is explicitly stopped, so return sticky. 
     return START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return mBinder; 
    } 

    // This is the object that receives interactions from clients. See 
    private final IBinder mBinder = new ServiceBinder(); 

    /** 
    * Show an alarm for a certain date when the alarm is called it will pop up a notification 
    */ 
    public void setAlarm(Calendar c) { 
     // This starts a new thread to set the alarm 
     // You want to push off your tasks onto a new thread to free up the UI to carry on responding 
     new AlarmTask(this, c).run(); 
    } 
} 

किसी भी मदद की सराहना की जाएगी - यह मैं शुरू करने के लिए कोशिश कर रहा हूँ सेवा के कोड है। धन्यवाद।

+0

मुझे लगता है कि आपको मैनिफेस्ट में .Notifications को हटा देना चाहिए। –

+0

अपनी सेवा कार्यान्वयन कोड पोस्ट करें। – yorkw

+0

मैंने कार्यान्वयन के साथ प्रश्न संपादित किया। – Tofira

उत्तर

1

ScheduleService (यानी पूर्ण पैकेज-नाम सहित) का पूर्ण योग्य वर्ग-नाम क्या है?

मैं क्योंकि अपनी AndroidManifest.xml फ़ाइल में इस पूछ रहा हूँ, आपकी सेवा के नाम .Notifications.ScheduleService है, जो थोड़ा अजीब लगता है:

यह मुझसे कहता है कि या तो

  • (के अंतिम भाग) पैकेज-नाम में ऊपरी-केस वर्ण ... इतना अच्छा नहीं है।
    यदि यह मामला है, तो मैं .notifications.ScheduleService की अपेक्षा करता हूं।
  • ScheduleService को Notifications.java नामक फ़ाइल के भीतर परिभाषित किया गया है। मैं .Notifications$ScheduleService की अपेक्षा करता हूं, यदि यह मामला है (अवधि के बजाय डॉलर चिह्न)।
1

क्या आपका मतलब bindService() झूठा रिटर्न देता है? onBind()IBinder प्रकार देता है।

ध्यान रखें कि सेवा बाध्यकारी कुछ समय लेता है। यदि आप बाध्यकारी के बाद कुछ क्रियाएं करना चाहते हैं तो आप इसे onServiceConnected() विधि में निष्पादित कर सकते हैं।

public void onServiceConnected(ComponentName className, IBinder service) { 

    mBoundService = ((ScheduleService.ServiceBinder) service).getService(); 
    Calendar c = new Calendar(); 
    mBoundService.setAlarm(c); 
} 

यदि आपको इस पर अधिक मार्गदर्शन की आवश्यकता है तो आपको हमें अपना गतिविधि कोड दिखाना होगा।

0

सेवा को बांधने के लिए आप एप्लिकेशन संदर्भ का उपयोग क्यों करते हैं? विधि बाइंड सेवा को ContextWrapper के माध्यम से बुलाया जाता है। यह मुद्दा नहीं हो सकता है लेकिन मैं उस स्थान पर संदर्भ साझा करता हूं जहां आप सेवा को बांधते हैं और जहां आपके पास कनेक्शन है।

boolean test = getApplicationContext().bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE); 

मुझे क्या करना चाहते हैं के बजाय अपने मामले में

boolean test = bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE); 

निम्नलिखित या आप आवेदन के भीतर एक वैश्विक संदर्भ रखने के लिए, अपने आवेदन फाइल और कॉल करने के लिए सब कुछ को स्थानांतरित करना चाहते हैं इसी तरह ऊपर सुझाए गए वही तरीके से।

समस्या आपके ऐप के पैकेज नाम पर भी हो सकती है और आपके मैनिफेस्ट में आपकी सेवा की घोषणा भी हो सकती है। यदि आप अनिश्चित हैं, तो सुनिश्चित करें कि मैनिफेस्ट में आपकी सेवा के लिए वैश्विक मार्ग देना है।

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