开发者

bindService force closes

I'm fairly new to Android programming and to Java in general, but I have extensive experience as a system-level C coder. I wanted to create an API or library that could be used by a suite of Android apps that I plan to develop and, after much research, decided that a bound service was the Android way of doing that (I would have normally created a .so or a .dll). If this assumption is incorrect, then my entire question could very well be moot.

At any rate, I created my service's framework in one project and I created an activity in another to test the service. I stubbed out the service's handleMessage() method with calls to Toast just to see if it was working and then added two buttons to my activity: one to bind the service and the second to unbind it. After about an hour of not being able to run my activity because it couldn't see my service, I figured out how to get Eclipse to see the source code of the service from the activity and thought I was well on my way. Then I ran the activity.

Everything looked good. Two buttons, one to bind, one to unbind. However, when I pressed the bind button, it force closed the app. I used the debugger to see what was going on and it is force closing on the bindService() call in my activity. Of course, the bindService() call was copied from another example in which a new Intent() was created on the same line, so I separated out the two lines of code and it is really dying when I attempt to create the Intent.

Here is the code for my activity:

package net.william.android.myAPI.tester;

import net.william.android.myAPI.MyApiService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;

public class MyApiTester extends Activity
{
    boolean mIsBound = false;

    private ServiceConnection mConnection = new ServiceConnection()
    {
        public void onServiceConnected(ComponentName className, IBinder service)
        {
            Toast.makeText(FaceClientTester.this, "Remote Service Connected", Toast.LENGTH_SHORT).show();
        }

        public void onServiceDisconnected(ComponentName className)
        {
            Toast.makeText(FaceClientTester.this, "Remote Service Disconnected", Toast.LENGTH_SHORT).show();
        }
    };

    public void doBindService(View view)
    {
        if (!mIsBound)
        {
            Toast.makeText(this, "Binding Service", Toast.LENGTH_SHORT).show();
            Intent myIntent = new Intent(MyApiTester.this, MyApiService.class); 
            bindService(myIntent, mConnection, Context.BIND_AUTO_CREATE);
            mIsBound = true;
        }
    }

    public void doUnbindService(View view)
    {
        if (mIsBound)
        {
            unbindService(mConnection);
            mIsBound = false;
            Toast.makeText(this, "Service Unbound", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        return;
    }
}

This is the code for my service:

package net.william.android.myAPI

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.widget.Toast;

public class MyApiService extends Service
{
    static final int API_FN_1   = 101;
    static final int API_FN_2   = 102;

    class MyApiHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case API_FN_1:
                    ApiFn1();
                    break;
                case API_FN_2:
                    ApiFn2();
                    break;
                default:
                    super.handleMessage(msg);
                    break;
            }
        }
    }

    final Messenger myMessenger = new Messenger(new MyApiHandler());

    @Override
    public void onCreate()
    {
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy()
    {
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        Toast.makeText(this, "Service Bound", Toast.LENGTH_SHORT).show();
        return myMessenger.getBinder();
    }

    private void ApiFn1()
    {
        Toast.makeText(this, "API Function 1", Toast.LENGTH_SHORT).show();
        return;
    }

    private void ApiFn2()
    {
        Toast.makeText(this, "API Function 2", Toast.LENGTH_SHORT).show();
        return;
    }
}

I changed some of the names of things, but that's really the meat of my code so far. Just a bunch of calls to Toast so that I could hopefully see that the thing is working.

All of the tutorials that I have found on the subject have completely ignored the manifest. I poked around a little last night trying to tweak manifest options thinking that that might have something to do with it, but to no avail. At any rate, for completeness, the following is the manifest code for the activity:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.william.android.myAPI.tester"
    androi开发者_如何学编程d:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyApiTester" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

and the service:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.william.android.myAPI"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <service android:name=".MyApiService"></service>
    </application>
</manifest>

I tried adding the following inside of the <service> block, but it didn't seem to help:

            <intent-filter>
                <action android:name="net.william.android.myAPI.MyApiService" />
            </intent-filter>

Anyway, this is my first post to your forum, so I apologize if there is too much or too little information, or if I missed some really easy fix out there somewhere on the web. I've been up most of the night reading all of the documentation on services that Google had and reading post after post in forums, so, at this point, I could very easily overlook something. Thanks in advance to anyone who finds the time to help!


If these are to be distributed as two separate APK files, as indicated via your pair of manifests, you cannot use the Intent constructor that takes a Java class as a parameter. The fact that this even compiles indicates your project setup is somewhat scary -- your client should not be within a country mile of your service from a build-time standpoint.

Add an <intent-filter> to your <service> advertising how you want to be connected to, and use that structure in your client for the Intent it creates.

For example, in this pair of sample projects, the service has:

<service android:name=".BshService">
    <intent-filter>
        <action android:name="com.commonsware.android.advservice.IScript" />
    </intent-filter>
</service>

and the client therefore uses:

new Intent("com.commonsware.android.advservice.IScript")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜