bindService强制关闭 [英] bindService force closes

查看:254
本文介绍了bindService强制关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新到Android编程和去渣一般,但我有丰富的经验作为一个系统级的C $ C $铬。我想创建一个可以通过一套房,我计划开发,并经过大量的研究Android应用程序中使用的API或库,决定绑定服务是这样做的Andr​​oid的方式(我会正常创建一个.so或.DLL)。如果这个假设是不正确,那么我的整个问题,很可能是没有实际意义。

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.

在任何情况下,我创建了服务的框架,在一个项目中,我在另一个创建了一个活动来测试服务。我掐灭服务的的handleMessage()方法调用吐司只是为了看它是否在工作,然后增加了两个按钮,我的活动:一个是绑定服务,第二次解除绑定。大约一个小时不能够运行我的活动,因为它看不到我的服务后,我想通了,如何让Eclipse来查看服务的来源$ C ​​$ c。从活动,以为我是好对我的方式。然后我跑了活动。

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.

一切都看起来很不错。两个按钮,一个结合,一个解除绑定。然而,当我pressed绑定按钮,它的力量关闭应用程序。我使用的调试,看看发生了什么事情,这是对我的活动bindService()调用强制关闭。当然,bindService()调用是从其中一个新的Intent()是在同一行上创造了另一个例子复制的,所以我分离出来code两行,这是的真正的快死的时候我试图创建的意图。

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.

下面是code代表我的活动:

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

这是code为我服务:

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

我改变了一些东西的名字,但是这真的是我的code的肉为止。只是一堆电话来敬酒的,这样我可以希望看到的事情正在工作。

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.

所有这一切我已经在这个问题中找到的教程已经完全忽略了体现。我戳周围有点昨晚试图调整清单选项认为,可能有一些用它做,但无济于事。无论如何,完整性,以下是清单code的活动:

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"
    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;服务与GT; 块,但它似乎并没有帮助:

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!

推荐答案

如果这些都被分布为两个单独的APK文件,通过您对舱单的指示,你不能使用意图构造函数的Java类作为参数。这甚至编译的事实表明你的项目设置是有点可怕的 - 你的客户不应该从构建时的角度来看你的服务的一个国家英里的范围内。

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.

添加&LT;意向滤光器&gt; 你的&LT;服务&GT; 广告要如何连接到,并使用该结构在客户端的意图它创建。

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.

例如,在此对样品 <一的HREF =htt​​ps://github.com/commonsguy/cw-advandroid/tree/master/AdvServices/RemoteService相对=nofollow>项目的,服务有:

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

这篇关于bindService强制关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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