Android的简单的服务没有启动 [英] Android simple service not starting

查看:241
本文介绍了Android的简单的服务没有启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是在问题的标题,我试图启动/绑定一个简单的服务,只要登录系统,它和它的不工作。我想与服务联接方法来做到这一点:

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

在活动:

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" />

没有错误,没有日志,没有任何反应。

推荐答案

您明确地启动该服务,并没有约束力。所以,你的服务连接code不会被解雇。

You are starting the service explicitly and not binding. So your service connection code won't be firing.

试试这个code来代替:

Try this code instead:

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

这篇关于Android的简单的服务没有启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆