【发布时间】:2014-04-25 06:15:35
【问题描述】:
我正在使用位置服务来获取位置更新。
我正在通过传递如下所示的待处理意图来启动服务
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!isMyServiceRunning()) {
mIntentService = new Intent(StartOrStopService.this,
LocationServices.class);
mPendingIntent = PendingIntent.getService(
StartOrStopService.this, 1, mIntentService, 0);
locationrequest = LocationRequest.create();
locationrequest.setInterval(1000*60);
locationClient.requestLocationUpdates(locationrequest,
mPendingIntent);
Toast.makeText(StartOrStopService.this, "Starting Service",
Toast.LENGTH_LONG).show();
} else
Toast.makeText(StartOrStopService.this,
"Service already running", Toast.LENGTH_LONG)
.show();
}
});
上面的代码运行良好,只需单击一个按钮即可启动服务。
我还有一个按钮,我用它来停止服务,由待定意图开始,下面是我尝试过的
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isMyServiceRunning()) {
StartOrStopService.this.stopService(new Intent(StartOrStopService.this,
LocationServices.class));
Toast.makeText(StartOrStopService.this,
"Stopping service",
Toast.LENGTH_LONG).show();
System.out.println("Stop service");
} else {
Toast.makeText(StartOrStopService.this,
"No Services are running presently",
Toast.LENGTH_LONG).show();
}
}
});
但我无法停止服务。有人可以建议我我在做什么错误。也给我一些建议??..
提前致谢:)
【问题讨论】:
标签: android service android-pendingintent