我想从我的活动叫我服务类的stopService()方法。 但我不知道如何从我的活动类访问stopservice方法。 我有以下code,但它不工作...........
这是主屏幕类:
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
enablecheck =(复选框)findViewById(R.id.enablecheck);
enablecheck.setOnCheckedChangeListener(新OnCheckedChangeListener()
{
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
// TODO自动生成方法存根
如果(enablecheck.isChecked()){
startService(新意图(HomeScreen.this,AutoService.class));
}其他
{
stopService(新意图(HomeScreen.this,AutoService.class));
}
}
});
}
这是服务类:
公共类汽车维修中心延伸服务{
私有静态最后字符串变量=汽车维修;
私人定时器定时;
私人TimerTask的任务;
@覆盖
公众的IBinder onBind(意向意图){
返回null;
}
@覆盖
公共无效的onCreate(){
Toast.makeText(这一点,自动服务创建,Toast.LENGTH_LONG).show();
Log.d(TAG的onCreate);
INT延迟= 5000; //延迟5秒。
INT周期= 5000; //重复每个秒。
定时器=新的Timer();
timer.scheduleAtFixedRate(任务=新的TimerTask(){
公共无效的run()
{
的System.out.println(完成);
}
},延迟,周期);
}
@覆盖
公共布尔stopService(意图名){
// TODO自动生成方法存根
timer.cancel();
task.cancel();
返回super.stopService(名称);
}
}
任何建议高度AP preciable。 感谢致敬 Mintu
解决方案我实际使用pretty的大同小异code,你上面。我的服务注册的清单如下
<服务机器人:名称=机器人service.MyService。:启用=真正的>
<意图过滤器的Android版本:标签=@字符串/ menuItemStartService>
<作用机器人:名称=it.unibz.bluedroid.bluetooth.service.MY_SERVICE/>
&所述; /意图滤光器>
< /服务>
在服务类我创建了一个按照常量字符串,标识的服务名称,如:
公共类的MyService延伸ForeGroundService {
公共静态最后弦乐MY_SERVICE =it.unibz.bluedroid.bluetooth.service.MY_SERVICE;
...
}
和从根据活动我把它与
startService(新意图(MyService.MY_SERVICE));
和停止
stopService(新意图(MyService.MY_SERVICE));
它完美。尝试检查你的配置,如果你不觉得有什么奇怪的尝试调试stopService是否获得的正常调用。
I am trying to call my service class's stopService() method from my activity. But I dont know how to access stopservice method from my activity class. I have the below code but its not working...........
This is HomeScreen class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
enablecheck = (CheckBox)findViewById(R.id.enablecheck);
enablecheck.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(enablecheck.isChecked()){
startService(new Intent(HomeScreen.this, AutoService.class));
}else
{
stopService(new Intent(HomeScreen.this, AutoService.class));
}
}
});
}
This is Service Class:
public class AutoService extends Service {
private static final String TAG = "AutoService";
private Timer timer;
private TimerTask task;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "Auto Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
int delay = 5000; // delay for 5 sec.
int period = 5000; // repeat every sec.
timer = new Timer();
timer.scheduleAtFixedRate(task = new TimerTask(){
public void run()
{
System.out.println("done");
}
}, delay, period);
}
@Override
public boolean stopService(Intent name) {
// TODO Auto-generated method stub
timer.cancel();
task.cancel();
return super.stopService(name);
}
}
Any suggestion highly appreciable. Thanks and Regards Mintu
解决方案I actually used pretty much the same code as you above. My service registration in the manifest is the following
<service android:name=".service.MyService" android:enabled="true">
<intent-filter android:label="@string/menuItemStartService" >
<action android:name="it.unibz.bluedroid.bluetooth.service.MY_SERVICE"/>
</intent-filter>
</service>
In the service class I created an according constant string identifying the service name like:
public class MyService extends ForeGroundService {
public static final String MY_SERVICE = "it.unibz.bluedroid.bluetooth.service.MY_SERVICE";
...
}
and from the according Activity I call it with
startService(new Intent(MyService.MY_SERVICE));
and stop it with
stopService(new Intent(MyService.MY_SERVICE));
It works perfectly. Try to check your configuration and if you don't find anything strange try to debug whether your stopService get's called properly.