分享 google 加上来自 android 的申请
Share in google plus from android application
我必须分享一些文本和来自 android 应用程序的 link,为此我编写了如下代码
PlusShare.Builder shareBuilder = new PlusShare.Builder(this);
shareBuilder.setType("text/plain");
shareBuilder.setText("text to be shared");
shareBuilder.setContentUrl(Uri.parse("link_to_share"));
Intent shareIntent = shareBuilder.getIntent();
startActivityForResult(shareIntent, SHARE_GOOGLE_PLUS_REQUEST_CODE);
这段代码工作正常,我可以分享给 google plus,并且在 onActivityResult() 中收到回电。我的问题是,如果用户未安装 google 另外,应用程序停止响应并退出。我该如何解决这个问题
试试下面的代码:
// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
mGoogleApiClient = new GoogleApiClient.Builder(ThirtyArticleDetail.this)
.addConnectionCallbacks(ThirtyArticleDetail.this)
.addOnConnectionFailedListener(ThirtyArticleDetail.this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
btnsharegplus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (!mGoogleApiClient.isConnected()) {
// Set sharing so that the share is started in onConnected.
mSignInClicked= true;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
} else {
Intent shareIntent = new PlusShare.Builder(ThirtyArticleDetail.this)
.setType("text/plain")
.setText("Welcome to the Google+ platform.")
.setContentUrl(Uri.parse(articleurl))
//.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent, 0);
}
}
});
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mSignInClicked = false;
// Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
Intent shareIntent = new PlusShare.Builder(this)
.setType("text/plain")
.setText("Welcome to the Google+ platform.")
.setContentUrl(Uri.parse(articleurl))
//.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent, 0);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//mGoogleApiClient.connect();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
if(!(mGoogleApiClient.isConnected()))
mGoogleApiClient.connect();
}
/**
* Sign-in into google
* */
/*private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
*/
/**
* Method to resolve any signin errors
* */
private void resolveSignInError() {
try {
if (mConnectionResult.hasResolution()) {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
}
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
catch (Exception e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
首先请通过以下链接了解 GoogleApiClient 实际上是什么:
https://developers.google.com/android/guides/api-client
http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/
https://developers.google.com/+/mobile/android/share/
编辑
不要忘记在 manifest.xml 中获得以下 权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
我必须分享一些文本和来自 android 应用程序的 link,为此我编写了如下代码
PlusShare.Builder shareBuilder = new PlusShare.Builder(this);
shareBuilder.setType("text/plain");
shareBuilder.setText("text to be shared");
shareBuilder.setContentUrl(Uri.parse("link_to_share"));
Intent shareIntent = shareBuilder.getIntent();
startActivityForResult(shareIntent, SHARE_GOOGLE_PLUS_REQUEST_CODE);
这段代码工作正常,我可以分享给 google plus,并且在 onActivityResult() 中收到回电。我的问题是,如果用户未安装 google 另外,应用程序停止响应并退出。我该如何解决这个问题
试试下面的代码:
// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
mGoogleApiClient = new GoogleApiClient.Builder(ThirtyArticleDetail.this)
.addConnectionCallbacks(ThirtyArticleDetail.this)
.addOnConnectionFailedListener(ThirtyArticleDetail.this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
btnsharegplus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (!mGoogleApiClient.isConnected()) {
// Set sharing so that the share is started in onConnected.
mSignInClicked= true;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
} else {
Intent shareIntent = new PlusShare.Builder(ThirtyArticleDetail.this)
.setType("text/plain")
.setText("Welcome to the Google+ platform.")
.setContentUrl(Uri.parse(articleurl))
//.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent, 0);
}
}
});
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mSignInClicked = false;
// Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
Intent shareIntent = new PlusShare.Builder(this)
.setType("text/plain")
.setText("Welcome to the Google+ platform.")
.setContentUrl(Uri.parse(articleurl))
//.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent, 0);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//mGoogleApiClient.connect();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
if(!(mGoogleApiClient.isConnected()))
mGoogleApiClient.connect();
}
/**
* Sign-in into google
* */
/*private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
*/
/**
* Method to resolve any signin errors
* */
private void resolveSignInError() {
try {
if (mConnectionResult.hasResolution()) {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
}
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
catch (Exception e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
首先请通过以下链接了解 GoogleApiClient 实际上是什么:
https://developers.google.com/android/guides/api-client
http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/
https://developers.google.com/+/mobile/android/share/
编辑
不要忘记在 manifest.xml 中获得以下 权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />