gpt4 book ai didi

java - 从 Activity 调用服务但在 logcat 中未获取任何内容?

转载 作者:太空宇宙 更新时间:2023-11-04 13:44:10 25 4
gpt4 key购买 nike

这有点像转发,对此我感到很抱歉,但上次我发布它时,我以为我已经把它记下来了,但显然没有。当我尝试调用服务时,我收到此错误,原因是:java.lang.IllegalStateException:GoogleApiClient尚未连接。因为我试图在 GoogleApiClient 连接之前调用 LocationServices 。因此,在稍微更改代码后,我不再收到错误,事实上,我不再在 logcat 中收到任何内容。这就是我从 SearchActivity.class 启动服务的方式:

SearchActivity.class

 button = (Button)findViewById(R.id.buttonPressed);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(getBaseContext(), LocationService.class));

}
});

这是服务:

LocationService.class

public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {

// LogCat tag
private static final String TAG = LocationService.class.getSimpleName();

private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;

private Location mLastLocation;

// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;

// boolean flag to toggle periodic location updates
private boolean mRequestingLocationUpdates = false;

private LocationRequest mLocationRequest;

// Location updates intervals in sec
private static int UPDATE_INTERVAL = 10000; // 10 sec
private static int FATEST_INTERVAL = 5000; // 5 sec
private static int DISPLACEMENT = 10; // 10 meters


@Override
public void onCreate() {
super.onCreate();

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();


}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

if (mGoogleApiClient != null) {
if(mGoogleApiClient.isConnected()) {
if (mLocationRequest != null) {
togglePeriodicLocationUpdates();
}
}
}



return START_NOT_STICKY;
}

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

protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FATEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}

private void togglePeriodicLocationUpdates() {
mGoogleApiClient.connect();
if(mGoogleApiClient.isConnected()) {
if (!mRequestingLocationUpdates) {

mRequestingLocationUpdates = true;

startLocationUpdates();

Log.d(TAG, "Periodic location updates started!");

} else {

mRequestingLocationUpdates = false;

// Stopping the location updates
stopLocationUpdates();

Log.d(TAG, "Periodic location updates stopped!");
}
}
}

protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}

protected void startLocationUpdates() {
mGoogleApiClient.connect();
if(mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
}

@Override
public void onConnected(Bundle arg0) {
createLocationRequest();
}

@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
+ result.getErrorCode());
}

@Override
public void onLocationChanged(Location location) {
// Assign the new location
mLastLocation = location;

Toast.makeText(getApplicationContext(), "Location changed!",
Toast.LENGTH_SHORT).show();
}

@Override
public boolean stopService(Intent name) {
return super.stopService(name);
}

AndroidManifest.xml

    </activity>
<service android:name=".LocationService">
</service>

编辑:

好吧,我想出了如何让它发挥作用。但现在我面临的问题是必须单击按钮两次才能正常启动。只需要稍微更改一下 onStartCommand() 即可。

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {

if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
if(mGoogleApiClient.isConnected()) {

if (mLocationRequest != null) {

togglePeriodicLocationUpdates();
}
}
}



return START_NOT_STICKY;
}

最佳答案

您应该阅读 GoogleApiClient 上的文档,特别是(针对当前的问题)connect() method 。我突出显示了相关部分:

Connects the client to Google Play services. This method returns immediately, and connects to the service in the background. If the connection is successful, onConnected(Bundle) is called and enqueued items are executed. On a failure, onConnectionFailed(ConnectionResult) is called.

If the client is already connected or connecting, this method does nothing.

因此,这就是您的代码当前发生的情况:

  1. 您单击按钮即可启动该服务。
  2. 您在 onCreate() 中创建 GoogleApiClient。
  3. 您在 onStartCommand 中调用 connect() ,当连接在后台继续时,它会立即返回。
  4. 您检查 isConnected(),但是,由于连接仍在继续,因此您什么都不做并从 onStartCommand 返回。
  5. 稍后,您单击按钮再次启动服务。
  6. 由于已经创建,所以直接进入onStartCommand。
  7. 此时,GoogleApiClient 已连接,因此其他一切都按预期运行。

你应该做的是实现 onConnected()。从那里,调用togglePeriodicLocationUpdates(),而不是在onStartCommand() 中执行此操作。这将实现你想要的:

  1. 点击按钮启动服务。
  2. 服务在 onCreate() 中创建 GoogleApiClient。
  3. 服务在 onStartCommand() 中启动连接。
  4. 在将来的某个时候,连接会建立,服务会从 onConnected() 调用togglePeriodicLocationUpdates()。

关于java - 从 Activity 调用服务但在 logcat 中未获取任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31032619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com