0

我的问题出在问题标题中,我正在尝试通过登录来启动/绑定一个简单的服务,但它根本不起作用。我想用 Service Connexion 方法来做:

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;
            }        
    }
}

在活动中:

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();
}

在清单中:

<service
    android:name="com.egdigital.testing.service.testServiceBound"
    android:enabled="true"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
</service>

我也试过这个

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

没有错误,没有日志,什么都没有发生..

4

2 回答 2

1

您正在显式启动服务而不是绑定。所以你的服务连接代码不会被触发。

试试这个代码:

Intent intenttest = new Intent(MainActivity.this, testServiceBound.class);
bindService(intenttest , _connectionStreamingtest   , Context.BIND_AUTO_CREATE);
于 2013-07-16T08:03:57.867 回答
0

好吧,至少我找到了!感谢所有的评论和回答=)

<service
    android:name="com.egdigital.testing.service.testServiceBound"
    android:enabled="true"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
</service>

这不在应用程序标签中!当然它需要在那里(1天才能找到这个..)

于 2013-07-16T08:20:03.450 回答