https://GitHub.com/gorbin/ASNE 库的简单示例项目
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:
To continue you need
##Integrating Facebook, Twitter and LinkedIn to your application
Create new Project in Android Studio
Let's save our social network keys in values/strings.xml
strings.xml(full source)
ASNE-tutorial
1646388738920557
BBQAUAVKYzmYtvEcNhUEvGiKd
byZzHPxE1tkGmnPEj5zUyc7MG464Q1LgNRcwbBJV1Ap86575os
75ubsp337ll7sf
8DVk4hi3wvEyzjbh
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)
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)
…
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
```Let's setup MainActivity.java We should set up onActivityResult method to catch responses after requesting login
MainActivity.java(full source)
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
Create MainFragment.java and begin transaction of this fragmetn in MainActivity.java
MainActivity.java(full source)
@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();
}
}Integrating of any social network is simple:
SocialNetworkManagermSocialNetworkManager = (SocialNetworkManager) getFragmentManager().findFragmentByTag(MAinActivity.SOCIAL_NETWORK_TAG);values.xml - note Facebook appId we used in AndroidManifest.xmlString 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";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))…Request login for every social networks
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))…onLoginSuccess(int networkId) or onError(int networkId, String requestID, String errorMessage, Object data) - lets show profile if login success and show Toast on errorMainFragment.java(full source)
…ProfileFragment.java get networkId from MainFragment.javaProfileFragment.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;
}
```Now via networkId we can get social network and request current user profile like:
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)
…socialNetwork.logout();
getActivity().getSupportFragmentManager().popBackStack();SocialNetworkManager like in step 8: GooglePlusSocialNetwork gpNetwork = new GooglePlusSocialNetwork暂无开放 Issues,或尚未同步最近议题。