【问题标题】:Android custom service - class implementationAndroid自定义服务-类实现
【发布时间】:2015-06-02 00:12:59
【问题描述】:

在决定如何实现我的后台自定义服务类时我有点困惑。

我需要什么:

  1. 位置更新和活动识别。
  2. REST API 调用和地理围栏 API。

我是 Android 后台处理的新手,不知道如何实现。

是否可以制作一个ThreadPoolExecutor(一个任务管理器类,runnables等)并处理线程间通信?

如果是,是否可以在 Thread 类中监听位置更新?

目前,我正在使用这个库 (Smart-location-lib) 来收听位置更新。

有没有更好的解决方案?

非常感谢任何帮助!

【问题讨论】:

    标签: java android multithreading location locationlistener


    【解决方案1】:

    这是在后台服务中使用 Google API 的最佳方式 而且您没有解释活动识别是什么意思?

    public class MyLocationService extends Service implements LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
    
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    
    public MyLocationService() {
    
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API).
                        addConnectionCallbacks(this).
                        addOnConnectionFailedListener(this)
                .build();
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mGoogleApiClient.connect();
        Toast.makeText(this, "Location services started", Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
    @Override
    public void onLocationChanged(Location location) {
        System.out.println("MyLocationService.onLocationChanged");
        // TODO with location updateshere
    }
    
    @Override
    public void onConnected(Bundle bundle) {
    
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(1000); // Update location every second
    
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        Location loc = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        sendMyLocUpdate(loc, UserProfile.DRIVER);
    }
    
    @Override
    public void onConnectionSuspended(int i) {
    
    }
    
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
    
    }
    
    @Override
    public void onDestroy() {
    
        Toast.makeText(this, "Location services stopped", Toast.LENGTH_LONG).show();
    
        Location loc = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        sendMyLocUpdate(loc, UserProfile.PASSENGER);
    
        mGoogleApiClient.disconnect();
        super.onDestroy();
    }
    

    }

    【讨论】:

    • 谢谢,但当我关闭应用程序时,此代码会重新启动。我怎样才能防止它重新启动?
    • 在 onStartCommand() 方法调用 service 的 startForeground() 和 onDestroy() 方法调用 stopForeground() 方法
    • "startForeground()" 用于向用户显示(通过通知)服务正在运行(播放音乐、广播等),这不是我的情况。
    • 如果不是你的情况,那么上面只有上面的服务对你有用,或者你可以添加你自己的逻辑。但是,每当启动此服务的活动销毁时,服务都会在活动销毁事件时重新启动
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2010-10-05
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    相关资源
    最近更新 更多