问题
My problem is in the question title, i'm trying to start/bind a simple service with just log in it and it's not working at all. I want to do it with Service Connexion method :
public class testServiceBound extends Service {
/**
* PRIVATE ATTRIBUTES
*/
// log
private static final String TAG = testServiceBound.class.getSimpleName();
private final Binder _binder = new testBinder();
@Override
public void onCreate() {
Log.i(TAG, "onCreate--");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand--");
return START_STICKY;
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy--");
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind--");
return _binder;
}
public class testBinder extends Binder {
public testServiceBound getServiceInstance() {
Log.i(TAG, "PlayerBinder--getServiceInstance--");
return testServiceBound.this;
}
}
}
In the activity :
private testServiceBound _testService;
public final ServiceConnection _connectionStreamingtest = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder testBinder) {
Log.i(TAG, "onServiceConnected--");
// service
testBinder binder = (testBinder) testBinder;
_testService = binder.getServiceInstance();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
Log.i(TAG, "onServiceDisconnected--");
_bound = false;
}
};
public void bindToService() {
Log.i(TAG, "bindToService--");
Intent intenttest = new Intent(MainActivity.this, testServiceBound.class);
startService(intenttest);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate--");
//
// service
bindToService();
}
In the manifest :
<service
android:name="com.egdigital.testing.service.testServiceBound"
android:enabled="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
</service>
and i tried with this too
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
No error, no log, nothing happens..
回答1:
You are starting the service explicitly and not binding. So your service connection code won't be firing.
Try this code instead:
Intent intenttest = new Intent(MainActivity.this, testServiceBound.class);
bindService(intenttest , _connectionStreamingtest , Context.BIND_AUTO_CREATE);
回答2:
OK at least i found ! Thanks for all the comments and answers =)
<service
android:name="com.egdigital.testing.service.testServiceBound"
android:enabled="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
</service>
this was not in the application tag !! And of course it needs to be there (1 day to find this..)
来源:https://stackoverflow.com/questions/17671102/android-simple-service-not-starting