如何检查后台服务(在Android上)是否正在运行?
我想要一个能够切换服务状态的Android活动 - 它可以让我打开它,如果它打开则关闭.
我还是新手.我使用stopService调用onDestroy方法.调用onDestroy方法,因为我可以看到toast消息.但是用于发送位置更新的无限for循环一直在运行.有什么我需要改变的吗?
public class myservice extends IntentService {
LocationManager locationManager ;
public myservice() {
super(myservice.class.getName());
}
@Override
protected void onHandleIntent(Intent intent) {
String numb=intent.getStringExtra("num");
for (;;) {
send(numb);
}
}
public IBinder onBind(Intent intent) {
return null;
}
public void send(String numb) {
locationManager = (LocationManager)this
.getSystemService(Context.LOCATION_SERVICE);
if (ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) {}
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Geocoder geocoder;
geocoder = new Geocoder(this, Locale.getDefault());
try {
Thread.sleep(3000);
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
Toast toast1 = Toast.makeText(this, "message …Run Code Online (Sandbox Code Playgroud)