在我目前的Android项目中,我正在启动服务startService(),然后我将绑定到服务bindService().(我这样做是因为我希望有一个我能够与之沟通的启动服务,没关系)
在Context绑定到Service之后,如果我理解了这一点,通常会调用onServiceConnected()我ServiceConnection (我之前创建的).
我可以假设,onServiceConnected()只有在执行了我的服务中的所有代码后才会调用onCreate它吗?
打开应用程序后,它会跳转到辅助功能设置。但onServiceConnected在我打开我的AccessibilityService(称为VoiceService)后,不会调用 。
你能告诉我我该怎么办吗?VoiceService尽管我在 XML 中启用了它,但它似乎无法启动。或者我需要以不同的方式将 theMainActivity与 the绑定?VoiceService
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wang.hearexpr">
<uses-permission
android:enabled="true"
android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".VoiceService"
android:enabled="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="@string/service_name">
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/voice_config"/>
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService"/>
</intent-filter>
</service>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
语音配置.xml
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/service_description"
android:packageNames="com.example.wang.hearexpr"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"/>
Run Code Online (Sandbox Code Playgroud)
语音服务.java
package com.example.wang.hearexpr;
import android.accessibilityservice.AccessibilityService; …Run Code Online (Sandbox Code Playgroud) 我目前正尝试在应用中添加应用内结算功能,以便用户可以进行小额捐款.我使用最新版本的Android Studio进行开发,并遵循本指南(一步一步,我正在做的每一个完全像提到...至少我认为我做:-)):https://developer.android.com /google/play/billing/billing_integrate.html
AIDL文件放在上面提到的位置(在src/maincom.android.vending.billing包中),我看到它是在该gen文件夹下生成的.
当我测试产品的检索时,我注意到该方法onServiceConnected从未被调用,它在活动中实现,如下所示:
IInAppBillingService mService;
ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name,
IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
};
Run Code Online (Sandbox Code Playgroud)
对服务的绑定是这样的(在同一个活动中):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donation);
Intent serviceIntent =
new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
}
Run Code Online (Sandbox Code Playgroud)
我注意到两件事:
... has leaked ServiceConnection ... that was originally bound here- 我看到有些人建议在应用程序上下文中替换使用bindService而不是活动,事实上如果我这样做,问题就消失了.但我认为这与我onServiceConnected从未打过电话的主要问题无关,而且为什么在官方指南中呼吁这项活动呢? …android serviceconnection in-app-billing android-billing onserviceconnected
首先,我要说的是,我已经在SO上发现了很多与此问题几乎相同的问题,但是没有一个(我能找到的)解决了这个问题.这些是我看过的一些:
ServiceConnection.onServiceConnected()在绑定到已启动的服务 之后永远不会调用onServiceConnected,在bindService方法之后从未调用过 ServiceConnection :: onServiceConnected,即使Context :: bindService返回true也未调用? OnServiceConnected未被调用
这是我的Android应用程序代码的一部分:
//Local service interface
private INetworkQueryService myNetworkQueryService = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Intent intent = new Intent(this, NetworkQueryService.class);
startService (intent);
bindService (new Intent(this, NetworkQueryService.class), myNetworkQueryServiceConnection,
Context.BIND_AUTO_CREATE);
}
//Service connection
private final ServiceConnection myNetworkQueryServiceConnection = new ServiceConnection() {
/** Handle the task of binding the local object to the service */
public void onServiceConnected(ComponentName className, IBinder service) {
myNetworkQueryService = ((NetworkQueryService.LocalBinder) service).getService();
// as soon as …Run Code Online (Sandbox Code Playgroud)