我可以为Android服务传入成员变量以供使用吗?
我想在另一个项目中重用一些服务层代码。我确实希望能够更改服务使用的一个成员变量,而无需使用不同的服务代码。我不知道如何做到这一点,因为我没有“创建”服务;清单和系统就是这样做的。因此,我无法在构造函数中传递某些内容 我很难找到能问这个问题的文字,所以也许一个简单的例子是最好的。我有一项服务,例如:我可以为Android服务传入成员变量以供使用吗?,android,service,android-manifest,Android,Service,Android Manifest,我想在另一个项目中重用一些服务层代码。我确实希望能够更改服务使用的一个成员变量,而无需使用不同的服务代码。我不知道如何做到这一点,因为我没有“创建”服务;清单和系统就是这样做的。因此,我无法在构造函数中传递某些内容 我很难找到能问这个问题的文字,所以也许一个简单的例子是最好的。我有一项服务,例如: public class MyService extends Service implements MyCommunicatorListener { ICommunicator _comm = ne
public class MyService extends Service implements MyCommunicatorListener {
ICommunicator _comm = new BlueToothCommunicator(); // great for project 1, not for project 2!
public void shutUpCommunicator() // example of using communicator
{
_comm.Shutup();
}
// MyCommunicatorListener methods
@Override
public void onPageReceived(Page page) { // example of listening to communicator
Log.d(TAG,"onPageReceived");
}
} // class greatly simplified...
一切都很好。该服务已启动,因为它已在清单中声明:
<service android:name="com.acme.servicelayer.MyService" android:process=":remote" >
<intent-filter>
<action android:name="com.acme.servicelayer.MyService" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</service>
我想使用:
ICommunicator _comm=新的WiFiCommunicator();//非常适合项目2,而不是项目2
1.
如何做到这一点并保持相同的代码库?有没有办法通过清单传递参数?
谢谢
Daveboot_completed是一个广播。服务将不会收到它。家是一个活动类别。服务将不会接收到它。请检查intent ONSTART命令和/或onBind,并根据intent调整代码。能否将部分或全部WifiCommunicator分解为一个包?如果是这样,您可以将该数据附加到意图,并根据需要使用onStartCommand/onBind配置或重新配置服务的*Communicator字段
ICommunicator _comm = new BlueToothCommunicator(); // great for project 1, not for project 2!