我有两个Android应用程序,分别称为“服务应用程序”和“客户端应用程序”。我的客户端应用程序需要绑定到我的服务应用程序导出的服务。
不幸的是,即使启动我的服务应用程序后,我也无法让我的客户端应用程序意识到我的服务应用程序导出的服务甚至存在。
我的服务应用程序的清单如下:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.service">
<application android:allowBackup="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name="com.example.service.App" android:theme="@style/AppTheme">
...
<service android:exported="true" android:name="com.example.service.MyService"/>
</application>
</manifest>
我的服务应用的MyService
类如下:
package com.example.service;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
public class MyService extends Service {
private final Messenger messenger = new Messenger(new MyHandler());
private static class MyHandler extends Handler {
public void handleMessage(Message message) {
//blah blah blah
}
}
public IBinder onBind(Intent intent) {
return this.messenger.getBinder();
}
}
我的客户端应用程序的服务绑定逻辑如下:
public class MainActivity extends AppCompatActivity {
public static final String tag = "MyClientApp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final Intent intent = new Intent();
intent.setClassName("com.example.service", "com.example.service.MyService");
boolean success = bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Log.e(tag, "success=" + success); //always prints false!
//I have also tried:
//1. getApplicationContext().bindService(...)
// (always returns false)
//2. startService(intent);
// (always returns null)
//3. getApplicationContext().startService(intent);
// (always returns null)
}
private Messenger mService;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
mService = new Messenger(iBinder);
Log.e(tag, "SERVICE CONNECTED");
//(method never called)
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.e(tag, "SERVICE DISCONNECTED");
mService = null;
//(method never called)
}
@Override
public void onBindingDied(ComponentName name) {
Log.e(tag, "BINDING DIED " + name);
//(method never called)
}
};
}
启动时:
adb shell am startservice -n "com.example.service/.MyService"
它给我以下错误消息:
Starting service: Intent { cmp=com.example.service/.MyService launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } }
Error: Not found; no service started.
在此问题上的帮助将不胜感激。作为参考,我正在三星Galaxy S8手机上同时运行这两个应用程序。
按照@pskink的要求,以下是服务应用的apk中的完整清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="2018003"
android:versionName="2018.003"
package="com.example.service">
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="27" />
<uses-permission
android:name="android.permission.VIBRATE" />
<application
android:theme="@ref/0x7f0c0006"
android:label="@ref/0x7f0b001d"
android:icon="@ref/0x7f070061"
android:name="com.example.service.App"
android:allowBackup="false">
<activity
android:label="@ref/0x7f0b001d"
android:name="com.example.service.BlipActivity"
android:launchMode="2"
android:configChanges="0x5a0"
android:windowSoftInputMode="0x13">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@ref/0x7f0b001d"
android:name="com.example.service.GameActivity"
android:exported="true"
android:launchMode="2"
android:configChanges="0x5a0"
android:windowSoftInputMode="0x13" />
<service
android:name="com.example.service.MyService"
android:exported="true" />
</application>
</manifest>