Android 警队停课

Android 警队停课,android,service,binding,android-intent,Android,Service,Binding,Android Intent,我对Android编程和Java一般来说都是相当陌生的,但我作为系统级C程序员有着丰富的经验。我想创建一个API或库,供我计划开发的一套Android应用程序使用,经过大量研究后,我决定绑定服务是Android的方式(我通常会创建一个.so或.dll)。如果这个假设是错误的,那么我的整个问题很可能是毫无意义的 无论如何,我在一个项目中创建了我的服务框架,在另一个项目中创建了一个活动来测试服务。我通过调用Toast来删除服务的handleMessage()方法,只是为了看看它是否工作,然后在我的活

我对Android编程和Java一般来说都是相当陌生的,但我作为系统级C程序员有着丰富的经验。我想创建一个API或库,供我计划开发的一套Android应用程序使用,经过大量研究后,我决定绑定服务是Android的方式(我通常会创建一个.so或.dll)。如果这个假设是错误的,那么我的整个问题很可能是毫无意义的

无论如何,我在一个项目中创建了我的服务框架,在另一个项目中创建了一个活动来测试服务。我通过调用Toast来删除服务的handleMessage()方法,只是为了看看它是否工作,然后在我的活动中添加了两个按钮:一个用于绑定服务,另一个用于解除绑定。大约一个小时后,我的活动无法运行,因为它看不到我的服务,我想出了如何让Eclipse从活动中看到服务的源代码,并认为我已经走上了正轨。然后我开始了这个活动

一切看起来都很好。两个按钮,一个用于绑定,一个用于解除绑定。然而,当我按下绑定按钮时,它强制关闭了应用程序。我使用调试器查看发生了什么,它正在强制关闭活动中的bindService()调用。当然,bindService()调用是从另一个示例中复制的,在该示例中,在同一行上创建了一个新的Intent(),因此我将这两行代码分开,当我尝试创建Intent时,它实际上正在消亡

以下是我的活动代码:

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;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.william.android.myAPI.tester"
    android: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>
这是我的服务代码:

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;
    }
}
我更改了一些东西的名称,但到目前为止,这确实是我代码的核心内容。只是一堆祝酒词,这样我就有希望看到事情进展顺利

我找到的关于这个主题的所有教程都完全忽略了清单。昨天晚上我四处闲逛了一会儿,试图调整清单选项,认为这可能与此有关,但没用。无论如何,为了完整起见,以下是活动的清单代码:

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;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.william.android.myAPI.tester"
    android: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>

以及服务:

<?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>

我尝试在
块中添加以下内容,但似乎没有帮助:

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


无论如何,这是我第一次在你的论坛上发表文章,所以如果有太多或太少的信息,或者如果我错过了一些非常简单的解决方法,我向你道歉。我整晚都在熬夜,阅读谷歌提供的所有服务文档,并在论坛上阅读一篇又一篇文章,所以,在这一点上,我很容易忽略了一些东西。提前感谢任何有时间帮忙的人

如果要将它们作为两个单独的APK文件分发(如清单对所示),则不能使用以Java类为参数的
Intent
构造函数。这甚至可以编译,这一事实表明您的项目设置有点可怕——从构建时的角度来看,您的客户机不应该在您服务的国家英里之内

将一个
添加到您的
中,说明您希望连接到的连接方式,并在客户端中使用该结构实现它创建的
意图

例如,在这对服务中,服务具有:

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

我建议发布LogCat中的堆栈跟踪,并指出上面代码中的哪一行与崩溃一致。我真的不建议对这样的批量诊断使用
Toast
,部分原因是我们看不到它们;使用
Log
上的方法将您自己的信息添加到LogCat。您指出您的代码“在尝试创建意图时正在消亡”,但我发现这很难相信,因为
Intent
构造函数不会真正崩溃。这就是为什么堆栈跟踪并指出崩溃的具体行很重要。@commonware-它在上面的意图行中消失。日志显示“找不到从方法net.william.android.myAPI.tester.myapietester.doBindService引用的类‘net.william.android.myAPI.MyApiService’”。然后显示“无法解析Lnet/william/android/myAPI/tester/myapiester;中的常量类16(Lnet/william/android/myAPI/MyApiService;)”。然后“在0x0011处替换操作码0x1c”接着是“Lnet/william/android/myAPI/tester/MyApiTester;.doBindService(Landroid/view/view;)V中的死代码0x0013-001c”。@commonware-我也会按照您的建议用日志方法替换Toast,但我不知道这会解决任何问题。请改变我调试的方式。非常感谢!正如您从我的原始帖子中看到的,昨晚我预感地在清单中添加了
,但未能将
意图
构造函数更改为使用字符串而不是类定义。它之所以编译,首先是因为Eclipse昨晚试图为我修复错误,并在tester项目中添加了对服务项目的引用,从而使它能够编译。我不是Eclipse的超级粉丝,因为我觉得它试图为我做很多事情。无论如何,我真的很感谢你的帮助!另外,我没有注册,所以我不能把你的评分从0提高到0,但还是要谢谢你。@William:“Eclipse昨晚试图帮我修复错误,并在tester项目中添加了对服务项目的引用,让它编译”——是的,有时候Eclipse太聪明了,不利于自己,更不用说我们自己了。:-)很高兴它对你有用!