我想用一个Activity来启动Service,具体说来是一个IntentService,但是有两个问题:
1)在启动时我的代码
private void switchService(boolean isEnable, String serviceName){
Intent intent = new Intent();
intent.setClassName(TestPhoneServiceActivity.this, serviceName);
if(isEnable){
startService(intent);
}
else{
if(isServiceRunning(serviceName)){
stopService(intent);
while(isServiceRunning(serviceName)){
}
Toast.makeText(this, "Service stopped Successfully!", Toast.LENGTH_LONG).show();
}
}
}
但是DDMS报错:Unable to start service Intent { cmp=com.xx.android/.AndroidPhoneService }: not found
但是这个路径是没错的,所以问题出在哪呢?还有,如果我需要调用一个系统服务,是不是也需要将其写到配置文件中啊?
2)之后我用intent.setClass的方法将这个IntentService启动之后,然后用ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE)去查看这个服务对象,结果发现没有在其中,难道是IntentService在结束后就自动destroy掉了吗,IntentService的生命周期跟普通的Service不一样吗?
1)在启动时我的代码
private void switchService(boolean isEnable, String serviceName){
Intent intent = new Intent();
intent.setClassName(TestPhoneServiceActivity.this, serviceName);
if(isEnable){
startService(intent);
}
else{
if(isServiceRunning(serviceName)){
stopService(intent);
while(isServiceRunning(serviceName)){
}
Toast.makeText(this, "Service stopped Successfully!", Toast.LENGTH_LONG).show();
}
}
}
但是DDMS报错:Unable to start service Intent { cmp=com.xx.android/.AndroidPhoneService }: not found
但是这个路径是没错的,所以问题出在哪呢?还有,如果我需要调用一个系统服务,是不是也需要将其写到配置文件中啊?
2)之后我用intent.setClass的方法将这个IntentService启动之后,然后用ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE)去查看这个服务对象,结果发现没有在其中,难道是IntentService在结束后就自动destroy掉了吗,IntentService的生命周期跟普通的Service不一样吗?
解决方案 »
- 关于System.exit(0)和android:clearTaskOnLaunch的问题
- android 关于gps状态改变 能触发什么广播
- android怎么点击屏幕就在屏幕上画一个半径为10的圆
- android文本转换及文字编码方式的问题
- Android平台来说UI控件都没有设计成为线程安全类型?
- 请教,android文件下载连接的地址这怎么写?
- 有安卓系统下的蒙古文软件吗?
- Android 中SocketClient client = new SocketClient("IP_ADDRESS",8000)出错
- Android中怎么让服务不依赖当前应用的主线程
- android shape 和 selector 混合使用没效果
- android中http请求的转发
- andorid-网络视频播放器
public IntentServiceName {
super(“IntentServiceName ”);
}
不用停止intentservice,它会自动停止的。
当描述service时能把你的manifest部分代码帖出来吗?
Intent intent = new Intent();
intent.setClassName(TestPhoneServiceActivity.this, MyIntentService.class);
if(isEnable){
startService(intent);
}
intent服务运行结束后,会自己停止。如果你在同一时间开启多个相同的intent服务,这写服务只能顺序执行,所以程序末尾的代码片段是多余的。
还有一定要在manifest文件中申明服务。