gpt4 book ai didi

java - 为什么我不能从我的 Activity 中调用此服务?

转载 作者:行者123 更新时间:2023-11-29 20:40:47 25 4
gpt4 key购买 nike

我试过以两种不同的方式调用此服务,但似乎没有用。

第一种方式是:

startService(new Intent(getBaseContext(), LocationService.class));

为此我收到一条错误信息`

引起:`java.lang.IllegalStateException:GoogleApiClient 尚未连接。

然后我尝试了这个:

Intent serviceIntent = new Intent();
serviceIntent.setAction("com.parseapp.eseen.eseen.service.LocationService");
startService(serviceIntent);

它也没有用,相反绝对没有任何反应,logcat 中没有任何显示。谁能帮忙?完整代码如下:

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) {
mGoogleApiClient.connect();
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 (!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();
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);
}

SearchActivity.class

button = (Button)findViewById(R.id.buttonPressed);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.parseapp.eseen.eseen.service.LocationService");
startService(serviceIntent);
}
});

AndroidManifest.xml

<service android:name=".LocationService">

<intent-filter>
<action android:name=".LocationService"> </action>
</intent-filter>

</service>


最佳答案

您第一次尝试使用类名启动服务成功了:

startService(new Intent(getBaseContext(), LocationService.class));

异常发生在服务开始执行后。在 GoogleApi 连接成功之前,即在调用 onConnected() 之后,您的代码无法使用 LocationServices。您的代码在到达 startLocationUpdates() 之前多次调用 connect(),但不等待调用 onConnected()。该方法是您在建立连接并且可以使用 LocationServices 时收到的通知。

demo code适用于 AsyncTask,而不是服务,但给出了如何使用 CountDownLatch 完成连接处理的想法。

关于java - 为什么我不能从我的 Activity 中调用此服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31018137/

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