PendingResult <placelikelihoodbuffer>第二次被执行

时间:2017-02-23 17:58:06

标签: android google-play-services google-places-api google-places

我正在尝试创建一个应用程序,其中用户当前位置将被发送到预定义的号码。我已经实现了ShakeDetector库。一切都运转良好,但是当我摇动设备时,我第二次得到地名。 [请看截图] 我在PendingResult部分的行上放了一个调试点。 第一次“for循环”没有执行,但是当我第二次摇动它时,for循环正在执行,而我正在获取这些位置。怎么了? PendingResult在android中作为asynctask工作吗?

PS:我在使用Google Fit API时遇到了类似的问题。当我按回按钮返回上一个活动时,PendingResult正在执行。我通过创建不同的方法来解决问题片段并为每个片段提供单个GoogleApiClient。但是你可以猜到这里没有这个范围。我应该怎么做才能帮忙。

first time run

second time run when I am getting the places

这是我的PhoneService.java代码,其中发生了一切。

public class PhoneService extends Service implements GoogleApiClient.OnConnectionFailedListener,GoogleApiClient.ConnectionCallbacks {
GoogleApiClient googleApiClient;
String place="";
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

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

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    ShakeDetector.create(this, new ShakeDetector.OnShakeListener() {
        @Override
        public void OnShake() {
            try{

                Toast.makeText(PhoneService.this,"Shake Detected",Toast.LENGTH_SHORT).show();
                buildApiClient();

            }catch (SecurityException e){

            }
        }
    });
    ShakeDetector.updateConfiguration(3f, 4);
    ShakeDetector.start();
    Toast.makeText(this,"Service started",Toast.LENGTH_SHORT).show();
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    ShakeDetector.stop();
}

public void buildApiClient(){
    googleApiClient= new GoogleApiClient
            .Builder(this)
            .addApi(Places.GEO_DATA_API)
            .addApi(Places.PLACE_DETECTION_API)
            .addConnectionCallbacks(PhoneService.this)
            .addOnConnectionFailedListener(PhoneService.this)
            .build();
    googleApiClient.connect();

}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Toast.makeText(PhoneService.this,"Connected",Toast.LENGTH_SHORT).show();
        try{
            if(googleApiClient.isConnected()){
                PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
                        .getCurrentPlace(googleApiClient, null);
                result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
                    @Override
                    public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
                        for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                            place=placeLikelihood.getPlace().getName()+"";
                            Log.d("PLACE",place);
                        }
                        likelyPlaces.release();
                        try{
                            SmsManager smsManager=SmsManager.getDefault();
                            smsManager.sendTextMessage("7098027655",null,"I am here at "+place+".Please pick me up.",null,null);

                        }catch (SecurityException e){

                        }
                    }
                });

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:7098027655"));
                callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(callIntent);
            }
        }catch (SecurityException e){

        }
}

@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(PhoneService.this,"Connection Suspended",Toast.LENGTH_SHORT).show();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Toast.makeText(PhoneService.this,"Connection Failed",Toast.LENGTH_SHORT).show();
}

}

1 个答案:

答案 0 :(得分:0)

每次用户摇动设备时,您都会创建并连接全新的GoogleApiClient。在服务的onCreate()方法中,您可能有更多的运气创建和连接客户端。