仍不能绑定到我的Andr​​oid服务 [英] Still cannot bind to my android service

查看:195
本文介绍了仍不能绑定到我的Andr​​oid服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的Andr​​oid和我移植了一个C#程序到Android。大部分会很好,但我不能够绑定到我的服务1的长时间运行的问题。
我想开始并绑定到服务,从服务接收sendBroadcast(意向)。意图包括数据包使用sendBroadcast(意向)在用户界面中显示,但该psumably返回一个空指针,$ P $因为它不连接到活动正常。
我跟了这么多不同的教程,包括本网站所有这一切似乎是很合理的,据报道工作的建议。

http://developer.android.com/reference/android/app/Service.html
http://devblogs.net/2011/01/04/bind-service-broadcast-receiver/
http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/

I am quite new to Android and I am porting a C# program to android. Most of it is going fine but I have a long running problem of not being able to bind to 1 of my services. I wanted to start and bind to the service to receive sendBroadcast(intent) from the service. The intent includes the data package for displaying in the UI using sendBroadcast(intent) but that returns a nullpointer, presumably because it is not connected to the activity properly. I have followed so many different tutorials including the suggestions on this site all of which seemed fairly logical and were reported working. Such as http://developer.android.com/reference/android/app/Service.html http://devblogs.net/2011/01/04/bind-service-broadcast-receiver/ http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/

该错误似乎是服务未正常启动......看到下面的堆栈跟踪:
ActivityThread.handleCreateService(ActivityThread $ CreateServiceData)行:2943
服务为空。的onCreate或onStartCommand()不会被调用的服务。

The error appears to be that the service is not starting properly...see the stack trace below: ActivityThread.handleCreateService(ActivityThread$CreateServiceData) line: 2943 Service is null. onCreate or onStartCommand() do not get called in the service.

不幸的是我不能用这种处理,因为服务类的构造函数不会从活动调用。
正如你可以看到我已经尝试了很多东西,做了一些认真读书,但显然我还是失去了一些东西。
我会给你code现在我想它生产上述栈跟踪的是哪个版本。这code不BUIL过去活动的OnCreate()!但是从bindservice()的布尔TMP返回true ??

Unfortunately I cannot use handlers for this because the constructor of the Service class is not called from the activity. As you can see I have tried many things and done some serious reading but obviously I am still missing something. I will give you what version of code I am currently trying which produced the stack trace above. This code doesn’t buil past the activities oncreate()! BUT the bool tmp from bindservice() returns true??

活动

        @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(D) Log.e(TAG, "+++ ON CREATE +++");

    // Various view initializations here

    Intent CuIntent = new Intent(this, Curve.class);
    CuIntent.setAction(".Core.CURVE_SERVICE");
    startService(CuIntent);
    Boolean tmp;
    tmp = bindService(CuIntent, CurveServiceConncetion, Context.BIND_AUTO_CREATE);


}

private ServiceConnection CurveServiceConncetion = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub

        CurveService = ((LocalBinder<Curve>) service).getService();

    }
    @Override
    public void onServiceDisconnected(ComponentName name) {

        // TODO Auto-generated method stub
        CurveService = null;
    }
};

与该服务的活动清单xml文件

The activity manifest xml file with the service

        <application android:label="@string/app_name" android:icon="@drawable/pi_icon_t">
    <activity android:label="@string/app_name" android:name=".BTUI" android:finishOnCloseSystemDialogs="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"></action>
            <category android:name="android.intent.category.LAUNCHER"></category>
        </intent-filter>
        </activity>
    <service android:name=".Core.Curve" />
</application>

该localBinder类(一个单独的类)从另一个教程和采取的是

The localBinder class (a separate class)is taken from another tutorial and is

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

}

这项服务看起来像这样

     public class Curve extends Service
{
    private IBinder mBinder = new LocalBinder<Curve>(this);

    @Override
    public void onCreate() {
        super.onCreate();
        String balls = "balls";
        Toast.makeText(this, "Curve Service created...", Toast.LENGTH_LONG).show();
    }


    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return mBinder;//
    }
    @Override
    public void onStart(Intent intent, int startId) {
         String balls = "balls";
            Toast.makeText(this, "Curve Service created...", Toast.LENGTH_LONG).show();

    }
    @Override
    public void onDestroy() {

       mBinder = null;
    }

这真的不能那么难我希望有人能看到我错了。

This really can't be that hard I hope someone can see where I am going wrong.

编辑我为这个职位选了个坏名声。绑定返回true,但它留下活动的onCreate()后立即崩溃。错误文本是在上面的介绍。

EDIT I picked a bad name for this post. The bind returns true but it crashes immediately after leaving the onCreate() of the activity. The error text is in the intro above.

推荐答案

很多code来看待低谷,但我有一个假设:

A lot of code to look trough but I have an assumption:

尝试删除:

CuIntent.setAction(".Core.CURVE_SERVICE");

从这里的意图过滤器:

And the intent filter from here:

<service android:name=".Core.Curve">
<intent-filter>
    <action android:name=".Core.CURVE_SERVICE"></action>
</intent-filter>
</service>

这篇关于仍不能绑定到我的Andr​​oid服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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