i was trying to integrate G+ button in ma app.The G+ buttons grayed out.and its not counting.how to turns red ?
public class HomeActivity extends SherlockActivity implements ConnectionCallbacks, OnConnectionFailedListener{
private static final String URL = "https://play.google.com/store/apps/details?id=com.phoneix.allu";
private static final int PLUS_ONE_REQUEST_CODE = 0;
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
private ProgressDialog mConnectionProgressDialog;
private PlusClient mPlusClient;
private ConnectionResult mConnectionResult;
private PlusOneButton mPlusOneStandardButton;
#Override
public void onCreate(Bundle pBundle) {
super.onCreate(pBundle);
setContentView(R.layout.dashboard);
mPlusOneStandardButton = (PlusOneButton) findViewById(R.id.plus_one_standard_button);
mPlusClient = new PlusClient.Builder(this, this, this)
.build();
}
#Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
#Override
protected void onResume() {
super.onResume();
// Refresh the state of the +1 button each time we receive focus.
mPlusOneStandardButton.initialize(URL, PLUS_ONE_REQUEST_CODE);
}
#Override
public void onDisconnected() {
// Nothing to do.
}
#Override
public void onConnectionFailed(ConnectionResult result) {
if (mConnectionProgressDialog.isShowing()) {
// The user clicked the sign-in button already. Start to resolve
// connection errors. Wait until onConnected() to dismiss the
// connection dialog.
if (result.hasResolution()) {
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e) {
mPlusClient.connect();
}
}
}
// Save the intent so that we can start an activity when the user clicks
// the sign-in button.
mConnectionResult = result;
}
public void onConnected(Bundle connectionHint) {
mPlusOneStandardButton.initialize(URL, PLUS_ONE_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
mConnectionResult = null;
mPlusClient.connect();
}
}
}
Try out this:
In xml file:
<com.google.android.gms.common.SignInButton
android:id="#+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="92dp" >
</com.google.android.gms.common.SignInButton>
</RelativeLayout>
In your activity get that button:
#Override
public void onCreate(Bundle pBundle) {
super.onCreate(pBundle);
setContentView(R.layout.dashboard);
findViewById(R.id.sign_in_button).setOnClickListener(new OnClickListener() {
#Override
public void onClick
(View v) {
Intent i = new Intent(getApplicationContext(),YourActivity.class);
startActivity(i);
}
});
}
The problem is with the latest update of google play services.
Once I uninstalled all updates of google play services from app's settings,all +1 buttons are showing up fine.
Let's hope google will fix their update.
The new Google Play update changed the interface of the plusOne button, hence breaking all plusOne button based on the "old" Google play service SDK.
If you checkout their samples, you'll discover they have changed the interface for the plusOnButton.initialize, which no longer takes a "plusOneClient", but a URL.
To fix, download the latest (v13) Google Play Services using your "Android SDK Manager", and import it back to your project.
Related
I am trying to use the Google Drive API for Android to open a file. From the following official tutorial, I have the following:
GoogleDriveActivity.class
public class GoogleDriveActivity extends Activity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private GoogleApiClient mGoogleApiClient;
private int REQUEST_CODE_RESOLUTION = 1;
private int REQUEST_CODE_OPENER = 2;
private ListView filesLv;
private DataBufferAdapter<Metadata> mResultsAdapter;
private String mNextPageToken;
private boolean hasMore;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_drive);
filesLv = (ListView) findViewById(R.id.listViewResults);
hasMore = true;
mResultsAdapter = new ResultsAdapter(this);
filesLv.setAdapter(mResultsAdapter);
filesLv.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int first, int visible, int total) {
if (mNextPageToken != null && first + visible + 5 < total) {
retrieveNextPage();
}
}
});
}
private void retrieveNextPage() {
// retrieve the results for the next page.
Query query = new Query.Builder()
.setPageToken(mNextPageToken)
.build();
Drive.DriveApi.query(mGoogleApiClient, query)
.setResultCallback(metadataBufferCallback);
}
private final ResultCallback<DriveApi.MetadataBufferResult> metadataBufferCallback = new
ResultCallback<DriveApi.MetadataBufferResult>() {
#Override
public void onResult(DriveApi.MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
return;
}
mResultsAdapter.clear();
mResultsAdapter.append(result.getMetadataBuffer());
mNextPageToken = result.getMetadataBuffer().getNextPageToken();
hasMore = mNextPageToken != null;
}
};
#Override
public void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
mGoogleApiClient.connect();
}
}
#Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (!connectionResult.hasResolution()) {
return;
}
try {
connectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (IntentSender.SendIntentException e) {
}
}
#Override
public void onConnected(Bundle bundle) {
retrieveNextPage();
}
#Override
public void onConnectionSuspended(int i) {
}
}
The ResultsAdapter.class:
public class ResultsAdapter extends DataBufferAdapter<Metadata> {
public ResultsAdapter(Context context) {
super(context, android.R.layout.simple_list_item_1);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(getContext(),
android.R.layout.simple_list_item_1, null);
}
Metadata metadata = getItem(position);
TextView titleTextView =
(TextView) convertView.findViewById(android.R.id.text1);
titleTextView.setText(metadata.getTitle());
return convertView;
}
}
I am including the dependency in the Gradle file like this:
compile 'com.google.android.gms:play-services-drive:7.8.0'
The Activity in the Manifest.xml looks like the following:
<activity
android:name="com.myproject.GoogleDriveActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<meta-data android:name="com.google.android.apps.drive.APP_ID" android:value="id=<google project number>"/>
</activity>
Please note that I have added the SHA1 to the Google API condole with the package name. Also, the fields in the content screen are filled out as explained here.
When I try to run this code, I keep getting the following error message in the onConnectionFailed callback:
{statusCode=INTERNAL_ERROR, resolution=null}
Any idea on what could be going wrong? I am not able to figure out what the problem is.
I found an answer. The problem was the debug key. Essentially, I ran the keytool command and generated the SHA1 which I then added to the API console on Google. I then ran the project from Android Studio. This was giving me the error.
I then created a new keystore from Android Studio -> Menu -> Build -> Generate signed apk. Ran the same command to generate SHA1 which I uploaded to the API console. Then with the updated apk file, I was able to get the contents of the file.
The problem was that the key could not be authenticated by Google.
I'm attempting to get Google Games to work with my application, but I'm having some issues. When I start my app, I call googleApiClient.connect(); on my onStart() method, along with some other code (posted below). Immediately when I start my app I get the log statement that onConnectionFailed was called and then the sign in process begins, the green fragment comes up and tells me to select my account. When I select my account, I get the log that is in onSignInFailed. I'm really confused what I'm doing wrong here. Any help would be greatly appreciated!
Here we go, I tried to only include the relevant bits:
public class SelectionScreen extends BaseGameActivity
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient googleApiClient;
private boolean mResolvingConnectionFailure = false;
private boolean mAutoStartSignInflow = true;
private boolean mSignInClicked = false;
private static int RC_SIGN_IN = 9001;
private boolean mIntentInProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selection_screen);
googleApiClient = new GoogleApiClient.Builder(this)
.addOnConnectionFailedListener(this).addConnectionCallbacks(this)
.addApi(Games.API).addScope(Games.SCOPE_GAMES).build();
}
#Override
protected void onStart() {
super.onStart();
googleApiClient.connect();
Log.d("Attempting to connect..", "");
}
#Override
protected void onStop() {
super.onStop();
googleApiClient.disconnect();
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("Connection failed! ", ":-(");
if (!mIntentInProgress && connectionResult.hasResolution()) {
try {
mIntentInProgress = true;
startIntentSenderForResult(connectionResult.getResolution()
.getIntentSender(), RC_SIGN_IN, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
mIntentInProgress = false;
googleApiClient.connect();
}
}
if (mResolvingConnectionFailure) {
return;
}
if (mSignInClicked || mAutoStartSignInflow) {
mAutoStartSignInflow = false;
mSignInClicked = false;
mResolvingConnectionFailure = true;
if (!BaseGameUtils.resolveConnectionFailure(this,
googleApiClient, connectionResult,
RC_SIGN_IN, "Signing in -- other error")) {
mResolvingConnectionFailure = false;
}
}
//show sign in button
ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, fragmentTwo);
ft.addToBackStack(null);
ft.commit();
}
#Override
public void onSignInButtonClicked(View view) {
googleApiClient.connect();
}
#Override
public void onSignInFailed() {
Log.d("sign in failed", "");
}
#Override
public void onSignInSucceeded() {
Log.d("sign in succeeded woo", "");
}
#Override
public void onActivityResult(int requestCode, int responseCode, Intent intent) {
super.onActivityResult( requestCode, responseCode, intent );
mHelper.onActivityResult( requestCode, responseCode, intent );
}
}
Is there some way I can get more detail about exactly what is causing the sign in error/connection error?
On the google play developer console it says my OAUTO 2.0 is fine, and I got the SHA thing from my keystore fine.. I'm sure I'm doing something retarded with my code, but I'm just not sure what. Any help would be appreciated, thanks!
edit Here are the metadata things of my manifest. Maybe I did these wrong? It looks correct from their guide.
<application>
//not showing all my activity stuff and application data, did I put the meta-data in the wrong place maybe?
<meta-data
android:name="com.google.android.gms.games.APP_ID"
//this is a number from my ids.xml file
android:value="#string/app_id" />
<meta-data
android:name="com.google.android.gms.version"
//generated by gradle, it finds the correct number (7327000)
android:value="#integer/google_play_services_version" />
</application>
Normally BaseGameActivity does all the heavy lifting for you so there's no need to do all the low level connection stuff in your custom Activity.
As stated here:
Subclasses only need to override the onSignInSucceeded and onSignInFailed abstract methods. To initiate the sign-in flow when the user clicks the sign-in button, subclasses should call beginUserInitiatedSignIn.
Also, are you sure you've set up Google Play Game Services using your local debug keystore?
I am trying to get the last known location using google services API, but after I build the GoogleApiClient, no callback method is ever fired.
My activity looks like that :
public class MainActivity extends Activity implements FragmentObserver, SessionSpotListObserver,
ConnectionCallbacks, OnConnectionFailedListener{
//Objects used for the location API
private Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
// Request code to use when launching the resolution activity
private static final int REQUEST_RESOLVE_ERROR = 1001;
// Unique tag for the error dialog fragment
private static final String DIALOG_ERROR = "dialog_error";
// Bool to track whether the app is already resolving an error
private boolean mResolvingError = false;
public static final String STATE_RESOLVING_ERROR = "resolving_state";
//Request code to use when launching the activity to fix the connection to google API
private static final int REQUEST_SOLVE_CONNEXION = 999;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//We make sure that google play service is available on the device
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS){
//We get a connection to the Google Play Service API to get the location of the user
buildGoogleApiClient();
}
else {
GooglePlayServicesUtil.getErrorDialog(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this),
this,
REQUEST_SOLVE_CONNEXION);
}
}
#Override
public void onConnectionFailed(ConnectionResult result) {
if (mResolvingError) {
// Already attempting to resolve an error.
return;
} else if (result.hasResolution()) {
try {
mResolvingError = true;
result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
} catch (SendIntentException e) {
// There was an error with the resolution intent. Try again.
mGoogleApiClient.connect();
}
} else {
// Show dialog using GooglePlayServicesUtil.getErrorDialog()
showErrorDialog(result.getErrorCode());
mResolvingError = true;
}
}
// The rest of this code is all about building the error dialog
/* Creates a dialog for an error message */
private void showErrorDialog(int errorCode) {
// Create a fragment for the error dialog
ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
// Pass the error that should be displayed
Bundle args = new Bundle();
args.putInt(DIALOG_ERROR, errorCode);
dialogFragment.setArguments(args);
dialogFragment.show(getFragmentManager(), "errordialog");
}
/* Called from ErrorDialogFragment when the dialog is dismissed. */
public void onDialogDismissed() {
mResolvingError = false;
}
/* A fragment to display an error dialog */
public static class ErrorDialogFragment extends DialogFragment {
public ErrorDialogFragment() { }
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the error code and retrieve the appropriate dialog
int errorCode = this.getArguments().getInt(DIALOG_ERROR);
return GooglePlayServicesUtil.getErrorDialog(errorCode,
this.getActivity(), REQUEST_RESOLVE_ERROR);
}
#Override
public void onDismiss(DialogInterface dialog) {
((MainActivity)getActivity()).onDialogDismissed();
}
}
#Override
public void onConnected(Bundle arg0) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
Log.d("API Connection", "The API has connected and the last location is :" + mLastLocation);
if (mLastLocation != null) {
}
}
#Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
}
/**
* Creates the connexion to the Google API. Once the API is connected, the
* onConnected method is called.
*/
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
I placed breakpoints on all callback methods, that is how I know that none is called.
Because at this stage I am not using Google Map Api, I did not register my app to get a key. Do I need to do that even if I just get the location ?
Don't hesitate to tell me if you need more info.
Thank you all.
You never call mGoogleApiClient.connect() after building your GoogleApiClient. Your onCreate() should instead be:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buildGoogleApiClient();
mGoogleApiClient.connect();
}
Note that there is no need to call GooglePlayServicesUtil.isGooglePlayServicesAvailable() if you are using GoogleApiClient as connect() includes that check as well.
consider calling onLocationChanged() and passing it's Location parameter to mLastLocation for continuous location update when the user location changes. Also you might want to reduce the drain on your battery by setting LocationRequest() interval and distance to a small value.
Hi I am using google plus. when I click button, it need to display email and other information. But problem is if user has multiple account, without clicking button, dialog is opening. After dismissing dialog and click button, once again dialog is opening and working fine. I couldn't find this strange issue.
When i launch screen, without clicking button, choose an account dialog is opening. How to solve this?
Here is my code: I am using fragments.
public class LoginFragment extends SherlockFragment implements OnClickListener,
ConnectionCallbacks, OnConnectionFailedListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Common.mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
Common.mGoogleApiClient.connect();
return view;
}
public void onStop() {
super.onStop();
if (Common.mGoogleApiClient.isConnected()) {
Common.mGoogleApiClient.disconnect();
}
}
}
Common:
public class Common {
static int RC_SIGN_IN = 0;
static String TAG = "MainActivity";
static int PROFILE_PIC_SIZE = 400;
static GoogleApiClient mGoogleApiClient;
static boolean mIntentInProgress;
static boolean mSignInClicked;
static ConnectionResult mConnectionResult;
static Activity con;
public Common(Activity c) {
con = c;
}
LoginActivity:
public class LoginActivity extends SherlockFragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
public void resolveSignInError() {
if (Common.mConnectionResult != null) {
if (Common.mConnectionResult.hasResolution()) {
try {
Common.mIntentInProgress = true;
Common.mConnectionResult.startResolutionForResult(
Common.con, Common.RC_SIGN_IN);
} catch (SendIntentException e) {
Common.mIntentInProgress = false;
Common.mGoogleApiClient.connect();
}
}
}
}
#Override
protected void onActivityResult(int requestCode, int responseCode,
Intent data) {
super.onActivityResult(requestCode, responseCode, data);
if (requestCode == Common.RC_SIGN_IN) {
if (responseCode != Common.con.RESULT_OK) {
Common.mSignInClicked = false;
}
Common.mIntentInProgress = false;
if (!Common.mGoogleApiClient.isConnecting()) {
Common.mGoogleApiClient.connect();
}
}
}
}
Here is screenshot where without clicking google plus button, dialog pop up.
I want to create a custom layout for my google plus button, any ideas? I've tried calling the OnClickEvent of the google plus button (that doesn't work) and I've tried changing the background image. Source code:
<com.google.android.gms.plus.PlusOneButton
xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
android:id="#+id/plus_one_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
plus:size="standard"
plus:annotation="inline"/>
holder.mPlusOneButton = (PlusOneButton) holder.content.findViewById(R.id.plus_one_button);
holder.mPlusOneButton.initialize("http://www.xxx.xx/", 0);
Add a custom button to your layout
Set OnClickListener to your custom button
Use the PlusClient as described here to handle the login procedure
As example I can provide my controller class for handling Google Plus login:
public class GooglePlusLoginController implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {
public static final int REQUEST_CODE_SIGN_IN = 100;
private PlusClient googlePlusClient;
private ConnectionResult connectionResult;
private Activity activity;
public GooglePlusLoginController(Activity activity) {
this.activity = activity;
googlePlusClient = new PlusClient.Builder(activity, this, this)
.setActions("http://schemas.google.com/AddActivity")
.setScopes(Scopes.PLUS_LOGIN) // Space separated list of scopes
.build();
googlePlusClient.connect();
}
// call this method in your click handler
public void login() {
try {
connectionResult.startResolutionForResult(activity, REQUEST_CODE_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
googlePlusClient.connect();
}
}
// call this method in your activity's onActivityResult
public void onActivityResult() {
if(!googlePlusClient.isConnected() && !googlePlusClient.isConnecting()) {
googlePlusClient.connect();
}
}
#Override
public void onConnected(Bundle bundle) {
// connected, you can now get user's data from
// googlePlusClient.getCurrentPerson()
}
#Override
public void onDisconnected() {
}
#Override
public void onConnectionFailed(ConnectionResult result) {
connectionResult = result;
}
private void logout() {
if(googlePlusClient.isConnected()) {
googlePlusClient.clearDefaultAccount();
googlePlusClient.disconnect();
googlePlusClient.connect();
}
}
}