所以我找到了关于GoogleApiClient的一些不太清楚的东西. GoogleApiClient有一个名为onConnected的函数,它在客户端连接时运行(肯定)
我得到了自己的函数:startLocationListening,最终在GoogleApiClient的onConnected函数上调用.
所以我的startLocationListening函数无法在没有GoogleApiClient连接的情况下运行.
代码和日志:
@Override
public void onConnected(Bundle bundle) {
    log("Google_Api_Client:connected.");
    initLocationRequest();
    startLocationListening(); //Exception caught inside this function
}
...
private void startLocationListening() {
    log("Starting_location_listening:now");
    //Exception caught here below:
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
   }
例外是:
03-30 12:23:28.947: E/AndroidRuntime(4936):     java.lang.IllegalStateException: GoogleApiClient is not connected yet.
03-30 12:23:28.947: E/AndroidRuntime(4936):     at com.google.android.gms.internal.jx.a(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936):     at com.google.android.gms.common.api.c.b(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936):     at com.google.android.gms.internal.nf.requestLocationUpdates(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936):     at hu.company.testproject.service.GpsService.startLocationListening(GpsService.java:169)
03-30 12:23:28.947: E/AndroidRuntime(4936): …android google-api-client google-play-services android-googleapiclient
我正在关注Google登录Android版.现在我可以获得idToken,但我之前使用的后端服务器期待访问令牌,因为我之前使用的是Google+登录.现在我不想改变我的服务器端.但我仍然可以如何使用Google登录并在我的Android应用程序中获取访问令牌,以便我可以将我的用户验证到我的后端服务器.
我之前使用的是GooglePlay Service 7.5.0,现在我正在使用GooglePlay Service最新的8.3.0.
android google-play-services android-googleapiclient google-signin
我希望能够在谷歌地图中截取某个位置的屏幕截图,而无需将谷歌地图实际加载到屏幕上,如果有一种方式,谷歌地图可以在后台拍摄截图,这将是很棒的.
我试图通过引用此链接在我的android项目中实现MVP模式:https://github.com/jpotts18/android-mvp
我已成功实现了view/presenter/interactor类.我不清楚
service拨打电话代码?由于我无法在演示者或者交互器类中获取上下文,因此我无法将
service调用放在那里
GoogleApiClient课程?由于
GoogleApiClient还需要运行上下文,因此在没有上下文的情况下也无法在展示器或交互器内实现
mvp android design-patterns android-service android-googleapiclient
我在尝试为Android Google登录注销时收到此错误消息:
Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
崩溃发生在DrawerActivity.java(下面)中,我在其中调用signOut()方法.
我已经查看了其他帖子中的解决方案,并试过它们无济于事:
java.lang.IllegalStateException:GoogleApiClient尚未连接
GoogleApiClient尚未连接异常 致命异常:java.lang.IllegalStateException GoogleApiClient尚未连接
MainActivity.java:
protected void onCreate(Bundle savedInstanceState) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
    //... other code for google sign in not shown
}
protected void onStart() {
    mGoogleApiClient.connect();
}
private void handleSignInResult(GoogleSignInResult result) {
    if (result.isSuccess()) {
        App.getInstance().setClient(mGoogleApiClient);
        //start DrawerActivity
    }
}
在DrawerActivity.java中(我要执行注销)
private void googleSignOut(){
    mGoogleApiClient = App.getInstance().getClient();
    Auth.GoogleSignInApi.signOut(mGoogleApiClient);
}
在我的App活动中扩展了Application(用于存储GoogleApiClient)
public class App extends Application {
    private GoogleApiClient mGoogleApiClient;
    private …java android google-api-client android-studio android-googleapiclient
当我在Activity中使用GoogleApiClient但将其移动到服务并且未调用onConnected时正在工作.
public class StepsMonitoringService extends Service implements GoogleApiClient.ConnectionCallbacks {
private GoogleApiClient mClient;
@Override
public IBinder onBind(Intent arg0) {
    return null;
}
@Override 
public void onCreate() {
    super.onCreate();
    mClient = new GoogleApiClient.Builder(this).addApi(Fitness.HISTORY_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
            .addConnectionCallbacks(this).build();
    mClient.connect();  
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;    
}
@Override
public void onDestroy() {
    super.onDestroy();
    mClient.disconnect();
}
@Override
public void onConnected(Bundle connectionHint) {
   // NOT being called!! WHY??
}
@Override
public void onConnectionSuspended(int cause) {
}
} …
在尝试几乎所有事情之后,我似乎无法在没有提取空对象引用的情况下获取映射.我试图将google mapfragment膨胀成片段,但每次我这样做时,我总是保留一个getmap null对象.
这是我的代码
在这一点上,我迫切需要任何帮助,我已经尝试过所有事情.
错误
  java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
            at com.gnumbu.errolgreen.importedapplication.ViewMapFragment.onViewCreated(ViewMapFragment.java:122)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:971)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
            at android.view.View.measure(View.java:17430)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
            at …我想要为了登录用户我必须使用这段代码:
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
注销
new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    disconnect();
                }
            });
但是当用户重新启动应用程序并且他已经登录(之前没有退出)是否可以检测到这个"当前登录"状态?
显然,可以将"登录"保存在应用程序的设置(共享首选项)中,但是可以使用google api进行检测吗?
android google-api-client android-googleapiclient google-signin googlesigninapi
我已经开始使用Dagger2来管理依赖项,我试图了解如何使用DI来提供单一的GoogleApiClient.对此的动机是:
我想在Application范围内提供Singleton GoogleApiClient.
你如何处理回调?无论您选择自动管理连接还是手动管理连接,都必须处理一些回调:
我按照Google的快速启动示例设置了GMail API:https://developers.google.com/gmail/api/quickstart/android
我的应用成功请求GET_ACCOUNTS权限,并允许用户选择他/她的Gmail帐户.所选帐户将保存在SharedPreferences中以供以后访问.
通过IntentService,我的应用程序会发送一封电子邮件.我按照此处的说明操作:https://developers.google.com/gmail/api/guides/sending,并根据需要包含activation.jar,additional.jar和mail.jar库.当我发送带有以下代码的电子邮件时,我得到一个UserRecoverableAuthUIException:
message = service.users().messages().send(userId, message).execute();
当我捕获异常时,获取与异常一起存储的意图,并将意图作为新活动启动,我会看到一个对话框,让我有机会允许我的应用程序使用我的GMail帐户发送电子邮件:
在此对话框上按"允许"后,我的应用程序会发送电子邮件而不会出现任何其他问题.我的应用也会显示在我的Google帐户的在线权限页面上,表示它有权发送电子邮件.
当我第一次获取用户的帐户名而不是等待发生异常时,有没有办法手动触发此对话框?
UPDATE
我已经能够提取存储在intent中的动作和数据,如果这有助于任何:
其中KEY是一系列字符,可能链接到我的帐户或是一个令牌.
编辑:
以下是创建Credentials对象并启动我正在使用的帐户选择器活动的代码:
private GoogleAccountCredential mCredential;
private static final String[] SCOPES = { GmailScopes.GMAIL_COMPOSE };
内部构造函数:
mCredential = GoogleAccountCredential.usingOAuth2(
                getApplicationContext(), Arrays.asList(SCOPES))
                .setBackOff(new ExponentialBackOff());
我在哪里获得帐户:
private void chooseGMailAccount() {
        String accountName = this.getSharedPreferences(getString(R.string.shared_pref_main),
                Context.MODE_PRIVATE)
                .getString(getString(R.string.srd_pref_gmail_account), null);
        if (accountName != null) {
            mCredential.setSelectedAccountName(accountName);
            configureGMailAPI();
        } else {
            startActivityForResult(
                    mCredential.newChooseAccountIntent(),
                    REQUEST_ACCOUNT_PICKER);
        }
    }
android android-authenticator google-oauth gmail-api android-googleapiclient