Created
September 20, 2012 10:32
-
-
Save Gopinathp/3755140 to your computer and use it in GitHub Desktop.
A public static method to figure out 50 Friends with Android phones and send them apprequests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void fbShareThisAppForAndroidUsers(final Facebook mFacebook, final Activity mActivity) { | |
AsyncTask<Object, Object, Object> asyncTask = new AsyncTask<Object, Object, Object>() { | |
private WeakReference<Activity> weakActivity; | |
private Facebook facebook; | |
@Override | |
protected Object doInBackground(Object... params) { | |
try { | |
facebook = mFacebook; | |
weakActivity = new WeakReference<Activity>(mActivity); | |
if (mFacebook.isSessionValid()) { | |
// String response = | |
// mFacebook.request("me/friends?fields=devices"); | |
String response = HttpRequest.get( | |
Constants.FB_GRAPH_API_URI + ("me/friends?fields=devices&limit=0&offset=0&access_token=") | |
+ mFacebook.getAccessToken()).body(); | |
if (response == null) | |
return null; | |
JSONArray array = new JSONObject(response).getJSONArray("data"); | |
ArrayList<String> fbIdsToPrefillArray = new ArrayList<String>(); | |
for (int i = 0; i < array.length(); i++) { | |
JSONObject object = (JSONObject) array.get(i); | |
if (object.has("devices")) { | |
String devicesNodeText = object.getString("devices"); | |
if (devicesNodeText.contains("Android")) { | |
fbIdsToPrefillArray.add(object.getString("id")); | |
} | |
} | |
} | |
return fbIdsToPrefillArray; | |
} | |
} catch (Exception e) { | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Object result) { | |
Bundle bundle = new Bundle(); | |
bundle.putString("message", "Try this " + weakActivity.get().getString(R.string.app_name) | |
+ " on your Android phone and never miss to wish your facebook friend even when you are offline."); | |
ArrayList<String> fbIdsArray = (ArrayList<String>) result; | |
if (fbIdsArray != null) { | |
StringBuilder toValueBuilder = new StringBuilder(); | |
int count = 50; | |
for (String aFbId : fbIdsArray) { | |
if (count-- > 0) { | |
toValueBuilder.append(aFbId); | |
toValueBuilder.append(","); | |
} | |
} | |
int lastIndexOfComma = toValueBuilder.lastIndexOf(","); | |
toValueBuilder.replace(lastIndexOfComma, lastIndexOfComma + 1, ""); | |
bundle.putString("to", toValueBuilder.toString()); | |
} | |
facebook.dialog(weakActivity.get(), "apprequests", bundle, listener); | |
super.onPostExecute(result); | |
} | |
}; | |
asyncTask.execute(null, null, null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment