java - 绑定到远程服务失败:无法启动服务意图
问题描述
我最近一直在玩一些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。
我已经使用installDebug
Gradle 任务安装了该服务,并且可以在“设置”中看到该应用程序。但是运行应用程序 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 中的错误总是一样的。
解决方案
推荐阅读
- electron-builder - 如何在 M1 Mac 上为 ARM64 架构构建 Quasar 应用程序
- asp.net-core - SignalR 客户端仅存在于 OnConnectedAsync 方法中
- git - Git忽略如何仅按名称获取“最后一个”文件
- sql-server - 使用命名空间解析 XML 中的数据
- html - ASP.NET Core 中的 foreach 循环使卡片垂直排列而不是水平排列
- c++ - C ++从int转换为字节数组,然后将该字节数组转换回int,导致溢出
- python - 匹配嵌套字典中的某些值但不匹配其他值
- javascript - 使用断言时如何使用异常
- wpf - 不使用 PaintSurface 事件在 SkiaSharp 中绘制的最有效方法
- ios - donePicker 无法识别的选择器发送到实例 UiTextField