我有一个启动服务的Android项目。然后我将其转换为Android库并将其添加到另一个Android项目中。 现在我想从我的主Android项目中自动启动该服务,但我迷路了。 这是我的清单代码(我添加了所有我从库中分类):
<service
android:name="com.test.mqttclientserver.MQTTService"
android:enabled="true"
android:exported="false"
android:label="Service" >
<intent-filter>
<action android:name="com.test.mqttclientserver.MQTTService" />
</intent-filter>
</service>
<activity
android:name="com.test.mqttclientserver.MQTTClient"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.test.mqttclientserver.MQTTNotifier"
android:label="@string/app_name" >
</activity>
在主要项目的活动中:
private boolean mBounded;
private MQTTService mService;
private MQTTConnectionStatus connectionStatus = MQTTConnectionStatus.INITIAL; ..... public IBinder onBind(Intent intent) {
return mBinder;
}
public MQTTConnectionStatus getConnectionStatus() {
return connectionStatus;
}
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(SplashScreen.this, "Service is disconnected", 1000)
.show();
mBounded = false;
mService = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(SplashScreen.this, "Service is connected", 1000).show();
mBounded = true;
LocalBinder mLocalBinder = (LocalBinder) service;
mService = (MQTTService) mLocalBinder.getService();
}
};
public void onStart(final Intent intent, final int startId) {
System.out.println("onStart loading...");
Intent mIntent = new Intent(this, MQTTService.class);
bindService(mIntent, mConnection, BIND_AUTO_CREATE);
System.out.println("onStart ending...");
}
protected void onStop() {
super.onStop();
if (mBounded) {
unbindService(mConnection);
mBounded = false;
}
}
这不够吗?只需一个提示即可。谢谢你的时间。