Android简单服务无法启动

时间:2013-07-16 07:59:48

标签: java android android-manifest android-service

我的问题在于问题标题,我正在尝试启动/绑定一个简单的服务,只需登录它,它根本不工作。我想用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" />

没有错误,没有记录,没有任何反应......

2 个答案:

答案 0 :(得分:1)

您正在明确启动服务而不是绑定。因此,您的服务连接代码不会被触发。

请尝试使用此代码:

Intent intenttest = new Intent(MainActivity.this, testServiceBound.class);
bindService(intenttest , _connectionStreamingtest   , Context.BIND_AUTO_CREATE);

答案 1 :(得分:0)

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

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

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