Java Android:无法绑定到已运行的服务,onServiceConnected未调用

Java Android:无法绑定到已运行的服务,onServiceConnected未调用,java,android,android-service,android-service-binding,Java,Android,Android Service,Android Service Binding,我正在运行服务(以前已成功地与主要活动绑定、启动和解除绑定): 当我成功关闭mainactivity(服务已解除绑定并保持运行)并希望以通知意图重新打开mainactivity时: Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.putExtra("Service", 1); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

我正在运行服务(以前已成功地与主要活动绑定、启动和解除绑定):


当我成功关闭mainactivity(服务已解除绑定并保持运行)并希望以通知意图重新打开mainactivity时:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("Service", 1);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); //tried also another flags
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
然后尝试绑定我正在运行的服务

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

    ...

    if (getIntent() != null && getIntent().hasExtra("Service")) {
        if (!bound) {
            bindService(new Intent(this, SturkoPlayerService.class), connection, 0);
        }
    }
}
my
Connection
变量的onServiceConnected函数未调用

private ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        SturkoPlayerService.LocalBinder mBinder = (SturkoPlayerService.LocalBinder) service;
        sturkoService = mBinder.getService();
        bound = true;
        sturkoService.registerClient(MainActivity.this);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        bound = false;
    }
};
舱单:

...
<activity android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:launchMode="singleTask"> //tried other modes too
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

<service
    android:name=".Service.SturkoPlayerService"
    android:enabled="true"
    android:exported="true"
    android:singleUser="true">
</service>
...
。。。
//也尝试过其他模式
...

p.S.:Catlog未显示任何内容

只有在以下情况下,服务才会运行:

  • 有1+个客户端绑定到它,或者

  • 您在其上调用了
    startService()
    ,但没有任何东西阻止它(例如,
    stopSelf()
    stopService()


否则,一旦最后绑定的连接解除绑定,服务将停止。

“服务解除绑定并保持运行”——除非调用
startService()
,否则服务将不会保持运行。如果您将
bindService()
调用中的
0
更改为
上下文。BIND\u AUTO\u CREATE
,现在一切正常,您的问题是您的服务在最初解除绑定时被破坏了。不,不是这样,我的服务会继续运行(我能听到它发出的声音)。当我创建它时,我使用Context.BIND\u AUTO\u CREATE(第一段代码),只有在重新绑定“它播放声音,我能听到”——您的服务没有播放声音时,我才使用0。安卓正在播放声音。仅仅因为您听到声音并不意味着您的服务正在运行。“当我创建它时,我使用Context.BIND\u AUTO\u CREATE(第一个代码块),我仅在重新绑定时使用0”--我建议您尝试将
0
替换为
Context.BIND\u AUTO\u CREATE
。如果它现在绑定,你的问题是你的服务在最初解除绑定时被破坏了,而且你的服务有bug,导致它在被破坏后无法阻止Android播放声音。我承认,你是对的@Commonware,现在我使用startService,然后绑定,在活动结束后,服务似乎是活的
...
<activity android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:launchMode="singleTask"> //tried other modes too
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

<service
    android:name=".Service.SturkoPlayerService"
    android:enabled="true"
    android:exported="true"
    android:singleUser="true">
</service>
...