百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

ASNETutorial

> 编程语言
开源

https://GitHub.com/gorbin/ASNE 库的简单示例项目

99 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

https://GitHub.com/gorbin/ASNE 库的简单示例项目

ASNETutorial

Simple example project for https://github.com/gorbin/ASNE library

Today social network integration to your android application is common practice - it makes user easily login to your app and share their actions. There are a lot of ways to do it - usually developers add native social network SDK or use API for every network. It provides login via installed social network application or native dialogs. You have to spend time and nerves to learn and use different social network SDKs.

What if you need to add one more social network for your application? Sometimes you have to reorganize or redo all your integrations. This leads to idea to create and implement common interface for all social networks. Fortunately there is an open source modular library ASNE that allows you to choose necessary social network and provides full sdk and common interface for most oftenly used requests(login, share, friendslist & etc) It saves your time and simplifies adding another networks in the future. Moreover you can easily add any other social network as new module - the similar way as it's done in other modules.

In this tutorial you can learn how easily integrate Facebook, Twitter in android application using ASNE modules. This is very basic tutorial with login, sharing link and showing friends list.

##Registering app - getting keys for your application In order to implement Social networks in your application you need keys to make API calls. So register a new social network application and get the keys. Check small tutorial how to get it:

  • Facebook
  • Twitter
  • LInkedIn

To continue you need

  • Facebook App ID
  • Twitter consumer key and consumer secret
  • LinkedIn consumer key and consumer secret

##Integrating Facebook, Twitter and LinkedIn to your application

  1. Create new Project in Android Studio

  2. Let's save our social network keys in values/strings.xml

    strings.xml(full source)

    xml
    
       
           ASNE-tutorial
       
           
               1646388738920557
           
           
               BBQAUAVKYzmYtvEcNhUEvGiKd
           
               byZzHPxE1tkGmnPEj5zUyc7MG464Q1LgNRcwbBJV1Ap86575os
           
           
               75ubsp337ll7sf
           
           
               8DVk4hi3wvEyzjbh
           
       
  3. Add permissions and meta data - open AndroidManifest.xml file and add uses-permission for INTERNET, ACCESS_NETWORK_STATE and add meta-data for facebook(add appId key)

    AndroidManifest.xml(full source)

    xml
    
    
    
        
        
    
        
            
                
                    
                    
                
            
    
            
        
    
    
  4. Set dependencies for asne-modules:

    Open Project Structure => choose your module and open Dependencies => Add new library dependency

Then search for asne and add asne-facebook, asne-twitter, asne-linkedin

or just add them manually to build.gradle

build.gradle(full source)

…
  1. Lets create some layouts Just login buttons in main fragment main_fragment.xml(full source)
    xml
    
    
    
        
        
        
    

Create simple profile card for user profile_fragment.xml(full source) ```xml

```

and save social networks colors to

color.xml(full source) ```xml

    #FFCCCCCC
    #4b4b4b
    #3b5998
    #55ACEE
    #287bbc

```
  1. Let's setup MainActivity.java We should set up onActivityResult method to catch responses after requesting login

    MainActivity.java(full source)

java
   public static final String SOCIAL_NETWORK_TAG = "SocialIntegrationMain.SOCIAL_NETWORK_TAG";
   
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       Fragment fragment = getSupportFragmentManager().findFragmentByTag(SOCIAL_NETWORK_TAG);
       if (fragment != null) {
           fragment.onActivityResult(requestCode, resultCode, data);
       }
   }

After every login form social networks send onActivityResult and we should check it and send to our SocialNetworkManager which deliver it to right SocialNetwork

  1. Create MainFragment.java and begin transaction of this fragmetn in MainActivity.java

    MainActivity.java(full source)

    java
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MainFragment())
                .commit();
        }
    
    }
  2. Integrating of any social network is simple:

    • Get SocialNetworkManager
    java
    mSocialNetworkManager = (SocialNetworkManager) getFragmentManager().findFragmentByTag(MAinActivity.SOCIAL_NETWORK_TAG);
    • Get keys from values.xml - note Facebook appId we used in AndroidManifest.xml
    java
    String TWITTER_CONSUMER_KEY = getActivity().getString(R.string.twitter_consumer_key);
    String TWITTER_CONSUMER_SECRET = getActivity().getString(R.string.twitter_consumer_secret);
    String TWITTER_CALLBACK_URL = "oauth://ASNE";
    String LINKEDIN_CONSUMER_KEY = getActivity().getString(R.string.linkedin_consumer_key);
    String LINKEDIN_CONSUMER_SECRET = getActivity().getString(R.string.linkedin_consumer_secret);
    String LINKEDIN_CALLBACK_URL = "https://asneTutorial";
    • Create chosen SocialNetworks with permissions
…
* Check if `SocialNetworkManager` is null init it and add `SocialNetworks` to it

```java
mSocialNetworkManager = new SocialNetworkManager();

mSocialNetworkManager.addSocialNetwork(fbNetwork);
mSocialNetworkManager.addSocialNetwork(twNetwork);
mSocialNetworkManager.addSocialNetwork(liNetwork);

//Initiate every network from mSocialNetworkManager
getFragmentManager().beginTransaction().add(mSocialNetworkManager, MAinActivity.SOCIAL_NETWORK_TAG).commit();
mSocialNetworkManager.setOnInitializationCompleteListener(this);
```
don't forget to implement `SocialNetworkManager.OnInitializationCompleteListener`
 
* If `SocialNetworkManager` - come from another fragment where we already init it - get all initialized social networks and add to them necessary listeners 

```java
if(!mSocialNetworkManager.getInitializedSocialNetworks().isEmpty()) {
    List socialNetworks = mSocialNetworkManager.getInitializedSocialNetworks();
    for (SocialNetwork socialNetwork : socialNetworks) {
        socialNetwork.setOnLoginCompleteListener(this);
    }
```
don't forget to implement `OnLoginCompleteListener`
* Now we need to catch callback after initializing of `SocialNetworks`

```java
@Override
public void onSocialNetworkManagerInitialized() {
    for (SocialNetwork socialNetwork : mSocialNetworkManager.getInitializedSocialNetworks()) {
        socialNetwork.setOnLoginCompleteListener(this);
        initSocialNetwork(socialNetwork);
    }
}
```
don't forget to implement `OnLoginCompleteListener`

Full onCreateView and onSocialNetworkManagerInitialized from MainFragment with initializing and setting listener to buttons

**MainFragment.java**(full [source](https://github.com/gorbin/ASNETutorial/blob/master/app/src/main/java/com/github/gorbin/asnetutorial/MainFragment.java))
…
  1. Request login for every social networks

    java
    SocialNetwork socialNetwork = mSocialNetworkManager.getSocialNetwork(networkId);
    socialNetwork.requestLogin();
    

Full OnClickListener loginClick with checking connection of social network and if social network connected - show ProfileFragment.java on click

 **MainFragment.java**(full [source](https://github.com/gorbin/ASNETutorial/blob/master/app/src/main/java/com/github/gorbin/asnetutorial/MainFragment.java))
…
  1. After social network login form we got callback onLoginSuccess(int networkId) or onError(int networkId, String requestID, String errorMessage, Object data) - lets show profile if login success and show Toast on error

MainFragment.java(full source)

…
  1. In ProfileFragment.java get networkId from MainFragment.java

ProfileFragment.java(full source) ```java public static ProfileFragment newInstannce(int id) { ProfileFragment fragment = new ProfileFragment(); Bundle args = new Bundle(); args.putInt(NETWORK_ID, id); fragment.setArguments(args); return fragment; }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
    networkId = getArguments().containsKey(NETWORK_ID) ? getArguments().getInt(NETWORK_ID) : 0;
    
}
```
  1. Now via networkId we can get social network and request current user profile like:

    java
    socialNetwork = MainFragment.mSocialNetworkManager.getSocialNetwork(networkId);
    socialNetwork.setOnRequestCurrentPersonCompleteListener(this);
    socialNetwork.requestCurrentPerson();

don't forget to implement OnRequestSocialPersonCompleteListener 13. After completing request we can use SocialPerson dadta to fill our profile view

ProfileFragment.java(full source)

…
  1. For logout you just need to use
java
socialNetwork.logout();
getActivity().getSupportFragmentManager().popBackStack();
  1. Truly, that's all - we integrate Facebook, Twitter and Linkedin and get user profile. You can add other social networks like Instagram or Google Plus just adding dependency for them and adding them to SocialNetworkManager like in step 8:
java
   GooglePlusSocialNetwork gpNetwork = new GooglePlusSocialNetwork

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Java

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言