我尝试为我的应用制作社交模块,类似于包装器,包含google +,Facebook和twitter集成模板。
我决定从Facebook开始,但我无法弄清我需要将两个模块的哪些数据放入Facebook API信息中心。
我有两个包名:
模块:com.example.denis.socialwrapper
app:com.example.denis.socialapp
我的模块项目只有一个班FacebookTemplate
我的应用项目有Activity
和Fragment
当我编译项目并按下LoginButton
时,它只显示progressBar。
这是我的FacebookTemplate类,位于模块中:
public class FacebookTemplate {
//template should has only one instance
private static FacebookTemplate facebookTemplate;
private String permissions;
private String requestFields;
private CallbackManager callbackManager; //fb callback
private LoginButton fbButton;
private FacebookCallback<LoginResult> callbackListener;
private GraphRequest.Callback graphAPIListener;
//========================================================================
//GET STATIC INSTANCE VIA DIFFERENT CONSTRUCTORS
//========================================================================
public static FacebookTemplate getInstance(){
if(facebookTemplate==null){
facebookTemplate = new FacebookTemplate();
facebookTemplate.setPermissions("user_profile");
facebookTemplate.setRequestFields("id,name");
return facebookTemplate;
}
return facebookTemplate;
}
public FacebookTemplate() {
}
//you must invoke this method before debug
public void initFacebookIntegration(Context context){
FacebookSdk.sdkInitialize(context);
}
//======================================================================
// CONFIGURING FACEBOOK INTEGRATION (FRAGMENT/ACTIVITY)
//======================================================================
public void configureFacebookUsingFragment(@NonNull Fragment fragment){
callbackManager = CallbackManager.Factory.create();
fbButton.setReadPermissions(permissions);
fbButton.setFragment(fragment);
// Callback registration
fbButton.registerCallback(callbackManager, callbackListener);
}
public void configureFacebookUsingActivity(){
callbackManager = CallbackManager.Factory.create();
fbButton.setReadPermissions(permissions);
// Callback registration
fbButton.registerCallback(callbackManager, callbackListener);
}
//======================================================================
// GETTING RESULT OF onActivityResult()
//======================================================================
public void throwToOnActivityResult(int requestCode,int resultCode,Intent data){
callbackManager.onActivityResult(requestCode, resultCode, data);
}
//======================================================================
// GETTERS AND SETTERS
//======================================================================
@NonNull
public CallbackManager getCallbackManager() {
return callbackManager;
}
public LoginButton getFbButton() {
return fbButton;
}
public void setPermissions(String permissions) {
this.permissions = permissions;
}
public String getPermissions() {
return permissions;
}
public String getRequestFields() {
return requestFields;
}
public void setRequestFields(String requestFields) {
this.requestFields = requestFields;
}
public void setFbButton(LoginButton fbButton) {
this.fbButton = fbButton;
}
public void setCallbackListener(FacebookCallback<LoginResult> callbackListener) {
this.callbackListener = callbackListener;
}
public void setGraphAPIListener(GraphRequest.Callback graphAPIListener) {
this.graphAPIListener = graphAPIListener;
}
public interface GraphAPIListener extends GraphRequest.Callback{
@Override
void onCompleted(GraphResponse graphResponse);
}
public interface FacebookCallbackListener extends FacebookCallback<LoginResult>{
@Override
void onSuccess(LoginResult loginResult);
@Override
void onCancel();
@Override
void onError(FacebookException e);
}
}
在Application类中我初始化Facebook SDK,这里是代码:
public class App extends Application {
private static FacebookTemplate FBTemplate;
@Override
public void onCreate() {
super.onCreate();
initFacebook();
}
private void initFacebook(){
FBTemplate = FacebookTemplate.getInstance();
FBTemplate.initFacebookIntegration(this);
}
public static FacebookTemplate getFacebookTemplate(){
return FBTemplate;
}
}
这是我的片段:
public class MainFragment extends Fragment {
@Bind(R.id.facebook_button)LoginButton fbButton;
private FacebookTemplate facebookTemplate;
private static MainFragment fragment;
public static MainFragment getInstance(){
if(fragment==null){
fragment = new MainFragment();
return fragment;
}
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.main_fragment,container,false);
ButterKnife.bind(this, v);
configureFacebookAPI(null,null);
return v;
}
private void configureFacebookAPI(@Nullable String permissions,@Nullable String requestFields){
facebookTemplate = App.getFacebookTemplate();
facebookTemplate.setFbButton(fbButton);
if(permissions!=null) facebookTemplate.setPermissions(permissions);
if(requestFields!=null) facebookTemplate.setRequestFields(requestFields);
facebookTemplate.configureFacebookUsingFragment(this);
facebookTemplate.setCallbackListener(callbackListener);
facebookTemplate.setGraphAPIListener(graphAPIListener);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebookTemplate.throwToOnActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}
FacebookTemplate.FacebookCallbackListener callbackListener = new FacebookTemplate.FacebookCallbackListener() {
@Override
public void onSuccess(LoginResult loginResult) {
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
};
FacebookTemplate.GraphAPIListener graphAPIListener = new FacebookTemplate.GraphAPIListener() {
@Override
public void onCompleted(GraphResponse graphResponse) {
}
};
}
模块的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.denis.socialwrapper">
<application android:allowBackup="true" android:label="@string/app_name"
android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_api_key"/>
</application>
应用程序的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.denis.socialapp" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:name=".App"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
仅尝试com.example.denis
。您只需要输入您的包裹名称。
答案 1 :(得分:0)
java&#34;包&#34;没关系。重要的是 AndroidManifest 包。从那里仔细检查您的包裹。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mygreatapp.droid" > <----- THIS PACKAGE
........
</manifest>
答案 2 :(得分:0)
我设置的权限不正确;修复解决了我的问题。