【问题标题】:bindService force closesbindService 强制关闭
【发布时间】:2011-08-12 14:59:13
【问题描述】:

总的来说,我对 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;
    }
}

这是我的服务的代码:

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

我更改了一些东西的名称,但到目前为止,这确实是我的代码的核心。只是给 Toast 打了一堆电话,这样我就有希望看到它正在工作。

我在该主题上找到的所有教程都完全忽略了清单。昨晚我四处寻找,试图调整清单选项,认为这可能与它有关,但无济于事。无论如何,为了完整起见,以下是活动的清单代码:

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

我尝试在 &lt;service&gt; 块内添加以下内容,但似乎没有帮助:

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

无论如何,这是我第一次在您的论坛上发帖,所以如果信息太多或太少,或者如果我错过了网络上某个地方的一些非常简单的解决方法,我深表歉意。我大部分时间都在熬夜阅读所有关于谷歌服务的文档,并在论坛上一个接一个地阅读,所以,在这一点上,我很容易忽略一些东西。提前感谢任何有时间提供帮助的人!

【问题讨论】:

  • 我建议从 LogCat 发布堆栈跟踪,并指出上面代码中的哪一行与崩溃一致。我真的不建议使用Toast 进行这样的批量诊断,部分原因是我们 看不到它们;使用Log 上的方法将您自己的信息添加到LogCat。您表示您的代码“在我尝试创建 Intent 时正在死去”,但我觉得这很难相信,因为 Intent 构造函数不会真正崩溃。这就是为什么堆栈跟踪和指出崩溃的特定行很重要的原因。
  • @CommonsWare - 它死在上面的 Intent 行。 LogCat 说“找不到类 'net.william.android.myAPI.MyApiService',引用自方法 net.william.android.myAPI.tester.MyApiTester.doBindService”。
  • 然后它说“无法在 Lnet/william/android/myAPI/tester/MyApiTester 中解析 const-class 16 (Lnet/william/android/myAPI/MyApiService;)”。
  • 然后“将操作码 0x1c 替换为 0x0011”,然后是“Lnet/william/android/myAPI/tester/MyApiTester;.doBindService (Landroid/view/View;)V 中的死代码 0x0013-001c”。
  • @CommonsWare - 我还将按照您的建议将 Toast 替换为 Log 方法,但我不知道这会解决任何问题。只需改变我调试的方式即可。

标签: android service binding android-intent


【解决方案1】:

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

&lt;intent-filter&gt; 添加到您的&lt;service&gt; 中,宣传您希望如何连接,并在您的客户端中使用该结构来创建它创建的Intent

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

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

因此客户使用:

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

【讨论】:

  • 非常感谢!正如您从我的原始帖子中看到的那样,我昨晚预感已将 &lt;intent-filter&gt; 添加到我的清单中,但未能将 Intent 构造函数更改为使用字符串而不是类定义。它首先编译的原因是因为 Eclipse 昨晚试图为我修复错误,并将对服务项目的引用添加到测试器项目中,让它编译。我不是 Eclipse 的忠实粉丝,因为我觉得它试图为我做很多 。无论如何,我真的很感谢你的帮助!
  • P.S.,我没有注册,所以我无法将你的评分从 0 提高到 0,但还是谢谢你。
  • @William:“Eclipse 昨晚试图为我修复错误,并将对服务项目的引用添加到测试项目中,让它编译”——是的,有时 Eclipse 实在是太糟糕了聪明是为了自己的利益,更不用说我们自己的利益了。 :-) 很高兴它对你有用!
猜你喜欢
  • 2015-04-03
  • 2013-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多