java - 安卓服务未启动

标签 java android service

我有调用服务的 Activity (主要 Activity )。该服务需要在后台运行并寻找附近的位置。

问题在于该服务没有执行任何操作(它没有输入 onStartCommand)。

如果您能帮助我理解问题并解决它,我会很高兴。

对服务的主要调用 -

startService(new Intent(this, BackgroundProcess.class));

服务 -

public class BackgroundProcess extends Service {

private static final String GOOGLE_API_KEY = My_Key;
GoogleMap googleMap;
double latitude = 0;
double longitude = 0;
private int PROXIMITY_RADIUS = 5000; // meter

@Override
public IBinder onBind(Intent arg0) {
    return null;
}


public int onStartCommand(Intent intent, int flags, int startId, Location location) {
    // Let it continue running until it is stopped.
    Log.d("Debag", "Hi");
    //show error dialog if GoolglePlayServices not available
    if (!isGooglePlayServicesAvailable()) {
        Log.d("Debag", "GoolglePlayServices not available");
    }

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, true);
    Log.d("Debag", "Hi");
    if (ActivityCompat.checkSelfPermission(BackgroundProcess.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(BackgroundProcess.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return 0;
    }
    location = locationManager.getLastKnownLocation(bestProvider);
    if (location != null) {
        onLocationChanged(location);
    }
    Log.d("Debag", "Hi");

    locationManager.requestLocationUpdates(bestProvider, 20000, 0, (LocationListener) this);

    // TODO Alloway check for the locations
    // TODO TIME LOOP
    Log.d("Debag", "Hi");
    String type = "shop"; // TODO place kind
    StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
    googlePlacesUrl.append("location=" + latitude + "," + longitude);
    googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
    googlePlacesUrl.append("&types=" + type);
    googlePlacesUrl.append("&sensor=true");
    googlePlacesUrl.append("&key=" + GOOGLE_API_KEY);

    GooglePlacesReadTask googlePlacesReadTask = new GooglePlacesReadTask();
    Object[] toPass = new Object[2];
    toPass[0] = googleMap;
    toPass[1] = googlePlacesUrl.toString();
    googlePlacesReadTask.execute(toPass);

    //SystemClock.sleep(30000); // 30 sec wait

    return START_STICKY;
}


private boolean isGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(BackgroundProcess.this);
    if (ConnectionResult.SUCCESS == status) {
        return true;
    } else {
        GooglePlayServicesUtil.getErrorDialog(status, null, 0).show();
        return false;
    }
}

public void onLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
}
}

最佳答案

您必须覆盖 Service.onStartCommand(Intent, int, int) ,但不知何故,您只实现了一个方法 onStartCommand(Intent, int, int, Location) ,该方法不是服务生命周期的一部分,因此永远不会自动调用。您应该将 Location location 声明为局部变量而不是参数,并使用 @Override 注释 onStartCommand() 以在签名不正确时收到警告匹配。

关于java - 安卓服务未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37237659/

相关文章:

android - 轮询与推送 - 有什么理由避免推送通知?

c# - Http 服务器 - 监听响应

部署到 Azure 的 WCF 服务

java - 如何查找 HTML 文件的大小和数据传输速率

java - 无法打印/获取二维数组的特定行

java - 获取模板时的速度抛出 NPE

ruby-on-rails - 如何将 Ruby on Rails 应用程序转换为 Windows/Linux 服务?

java - 如何从 lambda 表达式中收集列表

android - 如何从 EditText 中仅获取可见文本

android - "ionic cordova prepare android"继续尝试安装 iOS 插件导致在 Ubuntu 16.04 上构建失败