2012-05-14 12 views
7

मेरे पास यह सरल सेवा है जो उपयोगकर्ता के वर्तमान स्थान को प्रसारित करती है। मैं सेवा जीवन चक्र को नियंत्रित करने के लिए बाध्यकारी तंत्र का उपयोग करना चाहता हूं, लेकिन सेवा अभी शुरू नहीं हो रही है।सेवा के बाद सेवा (या कनेक्टिंग) नहीं बनाई जा रही है()

मैंने क्या गलत किया?

public class GPSActivity extends ListActivity { 
... 
protected void onResume() { 
     super.onResume(); 

     Log.i("Service", "Service bound"); 
     Intent intent = new Intent(this, LocationService.class); 
     bindService(intent, service_connection , Context.BIND_AUTO_CREATE); 
    } 

protected void onPause() { 
     if (dataUpdateReceiver!=null) 
      unregisterReceiver(dataUpdateReceiver); 
     unbindService(service_connection); 
     super.onPause(); 
    } 
class LocationServiceConnection implements ServiceConnection{ 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      Log.i("Service", "Service Connected"); 
     } 
       public void onServiceDisconnected(ComponentName name) { 

     } 
    } 
} 

LocalBinder.java

public class LocalBinder<S> extends Binder { 
    private String TAG = "LocalBinder"; 
    private WeakReference<S> mService; 


    public LocalBinder(S service){ 
     mService = new WeakReference<S>(service); 
    } 


    public S getService() { 
     return mService.get(); 
    } 
} 

LocationService.java

public class LocationService extends Service { 
    public void onCreate() { 
     initLocationListener(); 
     Log.i("Location Service","onCreate()"); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.i("Location Service", "Received start id " + startId + ": " + intent); 
     return START_NOT_STICKY; 
    } 

    private final IBinder mBinder = new LocalBinder<LocationService>(this); 
    @Override 
    public IBinder onBind(Intent intent) { 
     return mBinder; 
    } 
} 

AndroidManifest.xml

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    ... 

    <service android:name=".LocationService"> 
    </service> 
</application> 

संपादित करें: निकट के जवाब के लिए निश्चित धन्यवाद।

प्रकट प्रविष्टि एक लक्ष्य फ़िल्टर या सही नाम

<service 
    android:enabled="true" 
    android:name="com.android.gps.services.LocationService"> 
    <intent-filter> 
      <action android:name="com.android.gps.services.LocationService" /> 
    </intent-filter> 
</service> 

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

Intent intent = new Intent("com.android.gps.services.LocationService"); 
+0

क्या आपके 'सार्वजनिक IBinder onBind (आशय आशय)' में विधि 'LocationService.java' कैसा दिखता है? – Jens

+0

@ जेन्स मैंने ऑनबिंड() विधि को शामिल करने के लिए प्रश्न संपादित किया। – bughi

+0

एचएम। और आपको 'Log.i ("स्थान सेवा", "ऑनक्रेट()") भी नहीं मिल रहा है;' अपने लॉगकैट में लॉग इन करें? – Jens

उत्तर

3

onStartCommand onServiceConnected() और onServiceDisconnected() के लिए कॉलबैक संभाला केवल निष्पादित करेंगे अगर सेवा स्पष्ट रूप से शुरू हो गई है, तो ऐसा लगता है कि आप बस इसे बांधना चाहते हैं, जो ठीक है। मुझे नहीं लगता कि आपने सेवा कनेक्शन को ठीक से स्थापित किया है। मैं अपना स्टब प्रोग्राम पोस्ट कर रहा हूं जो दिखाता है कि सेवा से कैसे जुड़ना है और बाइंडर के माध्यम से सेवा में एक विधि को कॉल करना है। आप इसे चलाने और विभिन्न लॉग संदेशों के अनुक्रम को देखना पसंद कर सकते हैं। आपको अपने ब्रॉडकास्ट रिसीवर और ऑन-प्लेसमेंट चार्ज कोड को स्पष्ट रूप से आपके लिए उपयोगी बनाने की आवश्यकता होगी।

गतिविधि

package com.servtest.test; 

import com.servtest.test.LocationService.LocalBinder; 
import android.app.Activity; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 

public class ServiceTestActivity extends Activity { 

    boolean mServiceConnected = false; 
    boolean mBound = false; 
    private LocationService mLocnServ; 

    ServiceConnection mServconn = new ServiceConnection() { 
     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      Log.d("SVTEST", "Activity service connected"); 
      LocalBinder binder = (LocalBinder) service; 
      mLocnServ = binder.getService(); 
      // Can't call this methodInTheService UNTIL IT'S BOUND! 
      mLocnServ.methodInTheService(); 
      mBound = true; 
     } 

     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      Log.d("SVTEST", "Activity service disconnected"); 
      mBound = false; 
     } 
    }; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
    @Override 
    public void onStart() { 
     super.onStart(); 
     Log.d("SVTEST", "Activity onStart"); 
     mServiceConnected = bindService(new Intent(
       "com.servtest.test.LOCATIONSERVICE"), mServconn, 
       Context.BIND_AUTO_CREATE); 
    } 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.d("SVTEST", "Activity onResume"); 
    } 
    @Override 
    public void onPause() { 
     Log.d("SVTEST", "Activity onPause"); 
     super.onPause(); 
    } 
    @Override 
    public void onStop() { 
     Log.d("SVTEST", "Activity onStop"); 
     if (mBound) { 
      unbindService(mServconn); 
      mBound = false; 
     } 
     super.onStop(); 
    } 

} 

वें सेवा

package com.servtest.test; 

import android.app.Service; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.os.Binder; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 

public class LocationService extends Service implements LocationListener { 

    private final IBinder mBinder = new LocalBinder(); 

    @Override 
    public void onLocationChanged(Location arg0) {} 
    @Override 
    public void onProviderDisabled(String arg0) {} 
    @Override 
    public void onProviderEnabled(String arg0) {} 
    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {} 

    @Override 
    public IBinder onBind(Intent intent) { 
     Log.d("SVTEST", "Loc service ONBIND"); 
     return mBinder; 
    } 
    @Override 
    public boolean onUnbind(Intent intent) { 
     Log.d("SVTEST", "Loc service ONUNBIND"); 
     return super.onUnbind(intent); 
    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // Won't run unless it's EXPLICITLY STARTED 
     Log.d("SVTEST", "Loc service ONSTARTCOMMAND"); 
     return super.onStartCommand(intent, flags, startId); 
    } 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.d("SVTEST", "Loc service ONDESTROY"); 
    } 

    public class LocalBinder extends Binder { 
     LocationService getService() { 
      // Return this instance of LocalService so clients can call public methods 
      return LocationService.this; 
     } 
    } 

    public void methodInTheService() { 
     // A method you can call in the service 
     Log.d("SVTEST", "Loc service EXECUTING THE METHOD"); 
    } 
} 

प्रकट

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.servtest.test" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".ServiceTestActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service 
      android:enabled="true" 
      android:name="LocationService"> 
      <intent-filter> 
       <action android:name="com.servtest.test.LOCATIONSERVICE" /> 
      </intent-filter> 
     </service> 
    </application> 
</manifest> 

आशा इस मदद करता है

+0

धन्यवाद मैंने अपना जवाब संपादित किया जो मैंने गलत किया था – bughi

0

मैं था मेरी ActivityServiceConnection को लागू करने और इस तरह बाध्य:

bindService(new Intent(this, Service.class), this, Context.BIND_AUTO_CREATE); 

तब मेरी Activity

+0

इससे कोई फर्क नहीं पड़ता। – bughi

0

जब आप फोन bindService आप प्राप्त कर सकते हैं

ActivityManager java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.android.server.am.ActivityRecord$Token

logcat के उत्पादन की जाँच करें: त्रुटि है।

यह एंड्रॉइड की एक बग है।

हल करने के लिए यह, का उपयोग getApplicationContext().bindService(...)

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