private static final int GOOGLEPLUS_REQUEST_CODE = 1001; protected void onActivityResult(int requestCode, int resultCode, Intent data) { ... if ((requestCode == GOOGLEPLUS_REQUEST_CODE) && (resultCode == -1)) { //Do something if success } }
/** * Publish link in Google+ * @param text - message about link (may be changed or deleted by user) * @param link - http:// etc */ public final void googleplusPublish(String text, String link) { Intent shareIntent = new PlusShare.Builder(this) .setType("text/plain") .setText(text) .setContentUrl(Uri.parse(link)) .getIntent(); startActivityForResult(shareIntent, GOOGLEPLUS_REQUEST_CODE); }
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
private UiLifecycleHelper fbUIHelper; protected void onCreate(Bundle savedInstanceState) { ... fbUIHelper = new UiLifecycleHelper(this, null); fbUIHelper.onCreate(savedInstanceState); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { ... fbUIHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() { //Listener for Facebook-client if installed @Override public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) { toastMessage(" "); } @Override public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) { toastMessage(" , "); } }); protected void onResume() { ... fbUIHelper.onResume(); } protected void onSaveInstanceState(Bundle outState) { ... fbUIHelper.onSaveInstanceState(outState); } protected void onPause() { ... fbUIHelper.onPause(); } protected void onDestroy() { ... fbUIHelper.onDestroy(); }
/** * Publish link in FaceBook * @param name - title of block * @param caption - text on bottom of block * @param description - description of link (between title and caption) * @param link - http:// etc * @param pictureLink - http:// etc - link on image in web */ public final void facebookPublish(String name, String caption, String description, String link, String pictureLink) { if (FacebookDialog.canPresentShareDialog(getApplicationContext(), FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) { //Facebook-client is installed FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this) .setName(name) .setCaption(caption) .setDescription(description) .setLink(link) .setPicture(pictureLink) .build(); fbUIHelper.trackPendingDialogCall(shareDialog.present()); } else { //Facebook-client is not installed – use web-dialog Bundle params = new Bundle(); params.putString("name", name); params.putString("caption", caption); params.putString("description", description); params.putString("link", link); params.putString("picture", pictureLink); WebDialog feedDialog = new WebDialog.FeedDialogBuilder(this, Utility.getMetadataApplicationId(this), params) .setOnCompleteListener(new OnCompleteListener() { //Listener for web-dialog @Override public void onComplete(Bundle values, FacebookException error) { if ((values != null) && (values.getString("post_id") != null) && (error == null)) { toastMessage(" "); } else { toastMessage(" "); }; }; }) .build(); feedDialog.show(); } }
<activity android:name="com.vk.sdk.VKOpenAuthActivity"/>
private String appId = "1234567"; // Need to change to real app_id private static String vkTokenKey = "VK_ACCESS_TOKEN"; private static String[] vkScope = new String[]{VKScope.WALL}; private final VKSdkListener vkSdkListener = new VKSdkListener() { @Override public void onCaptchaError(VKError captchaError) { new VKCaptchaDialog(captchaError).show(); } @Override public void onTokenExpired(VKAccessToken expiredToken) { VKSdk.authorize(vkScope, true, false); } @Override public void onAccessDenied(VKError authorizationError) { new AlertDialog.Builder(SocialNetworkActivity.this) .setMessage(authorizationError.errorMessage) .show(); } @Override public void onReceiveNewToken(VKAccessToken newToken) { newToken.saveTokenToSharedPreferences(getApplicationContext(), vkTokenKey); } };
protected void onCreate(Bundle savedInstanceState) { ... VKUIHelper.onCreate(this); VKSdk.initialize(vkSdkListener, appId, VKAccessToken.tokenFromSharedPreferences(this, vkTokenKey)); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { ... VKUIHelper.onActivityResult(this, requestCode, resultCode, data); } protected void onResume() { ... VKUIHelper.onResume(this); } protected void onDestroy() { ... VKUIHelper.onDestroy(this); }
/** * Publish link in Vkontakte * @param message - message about link (may be changed or deleted by user) * @param link - http:// etc * @param linkName - title of link - not published (don't know why...) */ public final void vkontaktePublish(String message, String link, String linkName) { VKAccessToken token = VKAccessToken.tokenFromSharedPreferences(this, vkTokenKey); if ((token == null) || token.isExpired()) { VKSdk.authorize(vkScope, true, false); toastMessage(" . "); } else { new VKShareDialog() .setText(message) .setAttachmentLink(linkName, link) .setShareDialogListener(new VKShareDialog.VKShareDialogListener() { @Override public void onVkShareComplete(int postId) { toastMessage(" "); } @Override public void onVkShareCancel() { toastMessage(" "); } }).show(getSupportFragmentManager(), "VK_SHARE_DIALOG"); } }
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_share: View view = findViewById(R.id.action_share); showPopupMenu(view, "https://play.google.com/store/apps/details?id=ru.fantaversum.taleidoscope", " - ", " Google Play", " - . . .", "http://www.taleidoscope.ru/images/fb_logo.png", ": - . - . . .", " Google Play"); break; } return super.onOptionsItemSelected(item); }
public void showPopupMenu(View view, final String link, final String fb_name, final String fb_caption, final String fb_description, final String fb_pictureLink, final String message, final String linkName) { PopupMenu popup = new PopupMenu(this, view); popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.menu_facebook: facebookPublish(fb_name, fb_caption, fb_description, link, fb_pictureLink); return true; case R.id.menu_vkontakte: vkontaktePublish(message, link, linkName); return true; case R.id.menu_googleplus: googleplusPublish(message, link); return true; } return false; } }); popup.show(); }
Source: https://habr.com/ru/post/243411/
All Articles