Android Open Source - ShareSDK-for-Android Get Token Page






From Project

Back to project page ShareSDK-for-Android.

License

The source code is released under:

MIT License

If you think the Android project ShareSDK-for-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Offical Website:http://www.mob.com//ww  w .ja v  a  2s  .c om
 * Support QQ: 4006852216
 * Offical Wechat Account:ShareSDK   (We will inform you our updated news at the first time by Wechat, if we release a new version. If you get any problem, you can also contact us with Wechat, we will reply you within 24 hours.)
 *
 * Copyright (c) 2013 mob.com. All rights reserved.
 */

package cn.sharesdk.demo;

import java.util.ArrayList;
import java.util.HashMap;

import cn.sharesdk.framework.CustomPlatform;
import cn.sharesdk.framework.FakeActivity;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.TitleLayout;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.onekeyshare.ShareCore;
import android.graphics.drawable.ColorDrawable;
import android.os.Message;
import android.os.Handler.Callback;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

/** page to show how to authorize and get accesstoken by sharesdk */
public class GetTokenPage extends FakeActivity implements Callback,
    OnClickListener, PlatformActionListener {
  private TitleLayout llTitle;
  private AuthAdapter adapter;

  public void onCreate() {
    activity.setContentView(R.layout.page_get_access_token);
    llTitle = (TitleLayout) activity.findViewById(R.id.llTitle);
    llTitle.getBtnBack().setOnClickListener(this);
    llTitle.getTvTitle().setText(R.string.demo_get_access_token);

    ListView lvPlats = (ListView) activity.findViewById(R.id.lvPlats);
    lvPlats.setSelector(new ColorDrawable());
    adapter = new AuthAdapter(this);
    lvPlats.setAdapter(adapter);
  }

  public void onClick(View v) {
    if (v.equals(llTitle.getBtnBack())) {
      finish();
    }
  }

  public void onComplete(Platform plat, int action,
      HashMap<String, Object> res) {
    Message msg = new Message();
    msg.arg1 = 1;
    msg.arg2 = action;
    msg.obj = plat;
    UIHandler.sendMessage(msg, this);
  }

  public void onCancel(Platform plat, int action) {
    Message msg = new Message();
    msg.arg1 = 3;
    msg.arg2 = action;
    msg.obj = plat;
    UIHandler.sendMessage(msg, this);
  }

  public void onError(Platform plat, int action, Throwable t) {
    t.printStackTrace();

    Message msg = new Message();
    msg.arg1 = 2;
    msg.arg2 = action;
    msg.obj = plat;
    UIHandler.sendMessage(msg, this);
  }

  /** display authorize result by toast */
  public boolean handleMessage(Message msg) {
    Platform plat = (Platform) msg.obj;
    String text = MainActivity.actionToString(msg.arg2);
    switch (msg.arg1) {
      case 1: {
        // success
        text = plat.getName() + " get token: " + plat.getDb().getToken();
      }
      break;
      case 2: {
        // failed
        text = plat.getName() + " caught error";
      }
      break;
      case 3: {
        // canceled
        text = plat.getName() + " authorization canceled";
      }
      break;
    }

    Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
    return false;
  }

  private static class AuthAdapter extends BaseAdapter implements OnClickListener {
    private GetTokenPage page;
    private ArrayList<Platform> platforms;

    public AuthAdapter(GetTokenPage page) {
      this.page = page;

      // request platform list
      Platform[] tmp = ShareSDK.getPlatformList();
      platforms = new ArrayList<Platform>();
      if (tmp == null) {
        return;
      }

      for (Platform p : tmp) {
        String name = p.getName();
        if ((p instanceof CustomPlatform)
            || !ShareCore.canAuthorize(p.getContext(), name)) {
          continue;
        }
        platforms.add(p);
      }
    }

    public int getCount() {
      return platforms == null ? 0 : platforms.size();
    }

    public Platform getItem(int position) {
      return platforms.get(position);
    }

    public long getItemId(int position) {
      return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
        convertView = View.inflate(page.activity, R.layout.button_list_item, null);
      }

      Platform plat = getItem(position);
      Button btn = (Button) convertView.findViewById(R.id.btn);
      btn.setOnClickListener(this);
      btn.setText(page.activity.getString(R.string.get_token_format, getName(plat)));
      btn.setTag(plat);

      return convertView;
    }

    private String getName(Platform plat) {
      if (plat == null) {
        return "";
      }

      String name = plat.getName();
      if (name == null) {
        return "";
      }

      int resId = cn.sharesdk.framework.utils.R.getStringRes(page.activity, plat.getName());
      return page.activity.getString(resId);
    }

    public void onClick(View v) {
      Platform plat = (Platform) v.getTag();
      plat.setPlatformActionListener(page);
      plat.authorize();
    }

  }

}




Java Source Code List

cn.sharesdk.demo.AuthPage.java
cn.sharesdk.demo.CustomShareFieldsPage.java
cn.sharesdk.demo.CustomerPage.java
cn.sharesdk.demo.DemoPage.java
cn.sharesdk.demo.GetInforPage.java
cn.sharesdk.demo.GetTokenPage.java
cn.sharesdk.demo.JsonPage.java
cn.sharesdk.demo.Laiwang.java
cn.sharesdk.demo.MainActivity.java
cn.sharesdk.demo.MainAdapter.java
cn.sharesdk.demo.MyAdapter.java
cn.sharesdk.demo.MyQZoneWebShareAdapter.java
cn.sharesdk.demo.OneKeyShareCallback.java
cn.sharesdk.demo.ShareContentCustomizeDemo.java
cn.sharesdk.demo.SlidingMenuPage.java
cn.sharesdk.demo.WechatPage.java
cn.sharesdk.demo.YixinPage.java
cn.sharesdk.demo.wxapi.WXEntryActivity.java
cn.sharesdk.demo.yxapi.YXEntryActivity.java
cn.sharesdk.onekeyshare.CustomerLogo.java
cn.sharesdk.onekeyshare.CustomerLogo.java
cn.sharesdk.onekeyshare.EditPageFakeActivity.java
cn.sharesdk.onekeyshare.EditPageFakeActivity.java
cn.sharesdk.onekeyshare.FollowerListFakeActivity.java
cn.sharesdk.onekeyshare.FollowerListFakeActivity.java
cn.sharesdk.onekeyshare.OnekeyShareTheme.java
cn.sharesdk.onekeyshare.OnekeyShareTheme.java
cn.sharesdk.onekeyshare.OnekeyShare.java
cn.sharesdk.onekeyshare.OnekeyShare.java
cn.sharesdk.onekeyshare.PicViewer.java
cn.sharesdk.onekeyshare.PicViewer.java
cn.sharesdk.onekeyshare.PlatformListFakeActivity.java
cn.sharesdk.onekeyshare.PlatformListFakeActivity.java
cn.sharesdk.onekeyshare.Shake2Share.java
cn.sharesdk.onekeyshare.Shake2Share.java
cn.sharesdk.onekeyshare.ShareContentCustomizeCallback.java
cn.sharesdk.onekeyshare.ShareContentCustomizeCallback.java
cn.sharesdk.onekeyshare.ShareCore.java
cn.sharesdk.onekeyshare.ShareCore.java
cn.sharesdk.onekeyshare.ThemeShareCallback.java
cn.sharesdk.onekeyshare.ThemeShareCallback.java
cn.sharesdk.onekeyshare.theme.classic.EditPage.java
cn.sharesdk.onekeyshare.theme.classic.EditPage.java
cn.sharesdk.onekeyshare.theme.classic.FollowListPage.java
cn.sharesdk.onekeyshare.theme.classic.FollowListPage.java
cn.sharesdk.onekeyshare.theme.classic.PlatformGridView.java
cn.sharesdk.onekeyshare.theme.classic.PlatformGridView.java
cn.sharesdk.onekeyshare.theme.classic.PlatformListPage.java
cn.sharesdk.onekeyshare.theme.classic.PlatformListPage.java
cn.sharesdk.onekeyshare.theme.skyblue.EditPage.java
cn.sharesdk.onekeyshare.theme.skyblue.EditPage.java
cn.sharesdk.onekeyshare.theme.skyblue.FollowListPage.java
cn.sharesdk.onekeyshare.theme.skyblue.FollowListPage.java
cn.sharesdk.onekeyshare.theme.skyblue.PlatformGridViewAdapter.java
cn.sharesdk.onekeyshare.theme.skyblue.PlatformGridViewAdapter.java
cn.sharesdk.onekeyshare.theme.skyblue.PlatformListPage.java
cn.sharesdk.onekeyshare.theme.skyblue.PlatformListPage.java
cn.sharesdk.socialization.sample.MainActivity.java
cn.sharesdk.socialization.sample.MyPlatform.java
cn.sharesdk.socialization.sample.wxapi.WXEntryActivity.java
cn.sharesdk.socialization.sample.yxapi.YXEntryActivity.java