简单服务不会绑定:bindService return false,服务连接从未触发
Simple service won't bind : bindService return false, service connection never triggered
这是我第一次在 Android 中使用服务,我的服务不会绑定。
服务代码:
package mackosoft.almightymessage;
public final class MainService extends Service {
private final static String TAG = "Main Service";
private final IBinder binder = new MainServiceBinder();
@Override
public final void onCreate() {
super.onCreate();
Log.d(TAG, "Service created");
Toast.makeText(this, "Service created", Toast.LENGTH_SHORT).show();
}
@Override
public final int onStartCommand(final Intent intent, final int flags, final int startId) {
return START_STICKY;
}
@Override
public final void onDestroy() {
super.onDestroy();
Log.d(TAG, "Service created");
Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show();
}
@Override
public final IBinder onBind(final Intent intent) {
Toast.makeText(this, "Service binded", Toast.LENGTH_SHORT).show();
return this.binder;
}
public final class MainServiceBinder extends Binder {
final MainService getService() {
return MainService.this;
}
}
}
清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mackosoft.almightymessage" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:icon="@drawable/ic_logo"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MainService"
android:enabled="true" >
</service>
</Application
</manifest>
最后是 MainActivity 中我尝试绑定服务的部分:
public final class MainActivity extends AppCompatActivity {
@Override
protected final void onStart() {
super.onStart();
final Intent intent = new Intent(MainActivity.this, MainService.MainServiceBinder.class);
final boolean status = bindService(intent, this.connection, Context.BIND_AUTO_CREATE);
Log.d(TAG, "Service binded ? " + status);
}
private final ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.d(TAG, "Service connected !");
service = ((MainService.MainServiceBinder) iBinder).getService(); // service is a private member of MainActivity
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.d(TAG, "Service disconnected !");
}
};
}
运行 应用程序,Logcat 显示:
mackosoft.almightymessage D/MainActivity﹕ Service binded ? false
并且永远不会触发服务连接!
我尝试了多种解决方案:
- 将绑定调用移动到
Button.onClick()
并使用 getApplicationContext()
- 直接使用
getApplicationContext()
- 在清单中,更改为 ->
android:name="mackosoft.almightymessage.MainService"
- 也更改了
Intent
-> final Intent intent = new Intent(this, MainService.MainServiceBinder.class);
- 还尝试添加
this.startService(intent)
和 this.getApplicationContext()
以上所有操作均失败,并且日志中没有错误!
我一点运气都没有。
测试设备:Samsung Galaxy Note 3 运行 Cyanogen Mode 12.1 (Android 5.1)
Android Studio 1.2.1.1(稳定)
感谢帮助!!
final Intent intent = new Intent(MainActivity.this, MainService.MainServiceBinder.class);
应该是
final Intent intent = new Intent(MainActivity.this, MainService.class);
有关详细信息,请参阅 example provided in the docs
这是我第一次在 Android 中使用服务,我的服务不会绑定。
服务代码:
package mackosoft.almightymessage;
public final class MainService extends Service {
private final static String TAG = "Main Service";
private final IBinder binder = new MainServiceBinder();
@Override
public final void onCreate() {
super.onCreate();
Log.d(TAG, "Service created");
Toast.makeText(this, "Service created", Toast.LENGTH_SHORT).show();
}
@Override
public final int onStartCommand(final Intent intent, final int flags, final int startId) {
return START_STICKY;
}
@Override
public final void onDestroy() {
super.onDestroy();
Log.d(TAG, "Service created");
Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show();
}
@Override
public final IBinder onBind(final Intent intent) {
Toast.makeText(this, "Service binded", Toast.LENGTH_SHORT).show();
return this.binder;
}
public final class MainServiceBinder extends Binder {
final MainService getService() {
return MainService.this;
}
}
}
清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mackosoft.almightymessage" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:icon="@drawable/ic_logo"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MainService"
android:enabled="true" >
</service>
</Application
</manifest>
最后是 MainActivity 中我尝试绑定服务的部分:
public final class MainActivity extends AppCompatActivity {
@Override
protected final void onStart() {
super.onStart();
final Intent intent = new Intent(MainActivity.this, MainService.MainServiceBinder.class);
final boolean status = bindService(intent, this.connection, Context.BIND_AUTO_CREATE);
Log.d(TAG, "Service binded ? " + status);
}
private final ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.d(TAG, "Service connected !");
service = ((MainService.MainServiceBinder) iBinder).getService(); // service is a private member of MainActivity
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.d(TAG, "Service disconnected !");
}
};
}
运行 应用程序,Logcat 显示:
mackosoft.almightymessage D/MainActivity﹕ Service binded ? false
并且永远不会触发服务连接!
我尝试了多种解决方案:
- 将绑定调用移动到
Button.onClick()
并使用getApplicationContext()
- 直接使用
getApplicationContext()
- 在清单中,更改为 ->
android:name="mackosoft.almightymessage.MainService"
- 也更改了
Intent
->final Intent intent = new Intent(this, MainService.MainServiceBinder.class);
- 还尝试添加
this.startService(intent)
和this.getApplicationContext()
以上所有操作均失败,并且日志中没有错误!
我一点运气都没有。
测试设备:Samsung Galaxy Note 3 运行 Cyanogen Mode 12.1 (Android 5.1)
Android Studio 1.2.1.1(稳定)
感谢帮助!!
final Intent intent = new Intent(MainActivity.this, MainService.MainServiceBinder.class);
应该是
final Intent intent = new Intent(MainActivity.this, MainService.class);
有关详细信息,请参阅 example provided in the docs