【问题标题】:Cannot bind to service in android无法绑定到android中的服务
【发布时间】:2023-03-11 07:10:02
【问题描述】:

我正在处理一个更大的项目,但我陷入了一个绑定到 android 服务的非常简单的元素上。我的问题是我似乎无法绑定到服务。调用bindService() 时根本没有抛出任何错误,并且我使用了内部调试器和打印语句的手动跟踪,但我仍然无法理解导致此错误的原因。任何帮助表示赞赏! (这尤其令人恼火,因为它主要是来自 android 开发网站的 C+P,但仍然无法正常工作!)

主要活动:

package com.example.servicetest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

LocalService mService;
boolean mBound = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // START Activity
    Intent intent = new Intent(this, BindingActivity.class);
    startActivity(intent);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

绑定活动:

package com.example.servicetest;
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;

import com.example.servicetest.LocalService.LocalBinder;

public class BindingActivity extends Activity {
    LocalService mService;
    boolean mBound = false;

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
            System.out.println("End onServiceConnected");
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        System.out.println("onStart called");
        Intent intent = new Intent(this, LocalService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        this.startService(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }

    /** Called when a button is clicked (the button in the layout file attaches to
      * this method with the android:onClick attribute) */
    public void onButtonClick(View v) {

        if (mBound) {
            // Call a method from the LocalService.
            // However, if this call were something that might hang, then this request should
            // occur in a separate thread to avoid slowing down the activity performance.
             System.out.println("Button click");
            int num = mService.getRandomNumber();
            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
            System.out.println("" + num);
        } else {
             System.out.println("Not bound");
        }
    }


}

本地服务:

package com.example.servicetest;

import java.util.Random;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class LocalService extends Service {
    // Binder given to clients
    private final IBinder mBinder = new LocalBinder();
    // Random number generator
    private final Random mGenerator = new Random();

    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("onBind called");
        return mBinder;
    }

    /** method for clients */
    public int getRandomNumber() {
      return mGenerator.nextInt(100);
    }

}

最后是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.servicetest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.servicetest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.servicetest.BindingActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

【问题讨论】:

  • 为什么你认为它不起作用?
  • 我不太确定,我已经跟踪并观察了所有变量等,但绝不会抛出错误。我想它在 Binding Activity 类中,因为其他的都很简单,但是我检查了每个方法,找不到任何明显的问题
  • 我在你的 Manifest 文件中看不到你的Service 声明,这很重要,否则你的服务将无法工作!

标签: android android-intent service android-service


【解决方案1】:

我认为你需要在 Mainfest.xml 中添加你的服务

<service  android:name="LocalService" android:enabled="true"></service>

试一试,然后反馈给我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    相关资源
    最近更新 更多