首页 > 解决方案 > 绑定到远程服务失败:无法启动服务意图

问题描述

我最近一直在玩一些Android,并进行了以下设置:

我有应用程序 A,它是一项服务。这没有活动。这是它的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sandbox.sampleservice">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SampleService">

        <service
            android:name=".DummyService"
            android:exported="true"
            android:enabled="true">
        </service>

    </application>

</manifest>

这是Java代码:

package com.sandbox.sampleservice;

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.util.Log;

public class DummyService extends Service {
    private static final String TAG = "DummyService";

    static class MessageHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            // TODO: sort this later
            super.handleMessage(msg);
        }
    }

    Messenger mMessenger = null;

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "Binding");
        mMessenger = new Messenger(new MessageHandler());
        return mMessenger.getBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "No longer bound");
        return false;
    }
}

然后我有一个带有空活动的应用程序 B,我想将其绑定到应用程序 A 中的服务。这是它MainActivity

package com.sandbox.sampleclient;

import androidx.appcompat.app.AppCompatActivity;

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.util.Log;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "SampleClient";

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "Bound");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "Unbound");
        }
    };

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

        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.sandbox.sampleservice", ".DummyService"));
        boolean result = this.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
         if (result) {
                Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "failed", Toast.LENGTH_SHORT).show();
            }
    }
}

我在 Android Studio 附带的默认模拟器上运行 - API 30/x86。

我已经使用installDebugGradle 任务安装了该服务,并且可以在“设置”中看到该应用程序。但是运行应用程序 B,调用bindService()返回false(它显示failure吐司)。我在 logcat 中有以下消息:

2021-04-03 20:33:59.743 500-1764/system_process W/ActivityManager: Unable to start service Intent { cmp=com.sandbox.sampleservice/.DummyService } U=0: not found

我尝试了一些方法——在绑定时使用应用程序上下文而不是活动,使用完全限定的类名等等。行为没有变化,logcat 中的错误总是一样的。

标签: javaandroid

解决方案


推荐阅读