在Android

时间:2017-04-04 02:41:59

标签: java android singleton facebook-sdk-3.0

我有一个facebook初始化sdk调用,我需要它来初始化应用程序启动的时刻:

我想使用我的Application类来做到这一点。例如:

public class App extends Application {

@Override
public void onCreate() {
    super.onCreate();
    FacebookSdk.sdkInitialize(getApplicationContext());
}
}

我有facebook登录按钮的主要活动:

public class MainActivity extends AppCompatActivity {
@BindView(R.id.login_button)
LoginButton loginButton;

private CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    callbackManager = CallbackManager.Factory.create();
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException error) {

        }
    });
}
}

如何调用我的应用程序单例?如何使其onCreate()工作?

3 个答案:

答案 0 :(得分:8)

扩展android.app.Application类时,不必显式调用onCreate。但请确保在AndroidManifest.xml中指定它, 像这样 android:name =“完全限定名称” 例如:android:name =“com.example.MyApplication”

您不必实施单例模式,因为应用程序已经是单个,只有一个应用程序实例。 并且,要获取 的Application实例或您定义的应用程序的任何自定义子项,您只需调用 context.getApplication()

参考https://developer.android.com/reference/android/app/Application.html

答案 1 :(得分:4)

要使您的App类单例遵循Singleton设计模式:

public class App
{
// Create the instance
private static App instance;
public static App getInstance()
{
     if (instance== null) {
            synchronized(App.class) {
                    if (instance == null)
                            instance = new App();
                    }
            }
            // Return the instance
            return instance;
}

private App()
{
    // Constructor hidden because this is a singleton
}

public void initFBSdk()
{
    FacebookSdk.sdkInitialize(getApplicationContext());
}

}

然后在任何其他类中使用它:

App.getInstance().initFBSdk();

如果这是你要求的......

答案 2 :(得分:-2)

我认为你正试图在java中创建一个线程安全的单例。 以下是App类所需的代码:

public class App extends Application 
{
    private static App instance;

    public static App getAppInstance() { return instance; }

    @Override
    public void onCreate() 
    {
        super.onCreate();
        instance = this;
    }   

    public void initFacebookSdk()
    {
        FacebookSdk.sdkInitialize(getApplicationContext());
    }
}

这只是对自己的引用。我们想要实现Singleton for Multithreading。然后我们将创建另一个类,在其构造函数中封装App类,如下所示:

public class SingletonApp
{
    private static App appInstance = null;
    private static SingletonApp instance = null;
    private static final Object mutex = new Object();

    public static SingletonApp getInstance() 
    {
        SingletonApp r = instance;
        if (r == null) {
            synchronized (mutex) {  // While we were waiting for the sync, another 
                r = instance;       // thread may have instantiated the object.
                if (r == null) {  
                    r = new SingletonApp();
                    instance = r;
                }
            }
        }

        return r;
    }

    private SingletonDemo()
    {
       // Direct access the Application context calling
       appInstance = App.getAppInstance();
    }

    public static getAppInstance()
    {
       return getInstance().appInstance;
    }
}

然后在MainActivity课程中,您可以导入静态实例并在代码中使用它:

import static your.package.name.SingletonApp.getAppInstance;

例如,您可以在MainActivity类中使用它,只需调用方法initFacebookSdk(),如下所示:

public class MainActivity extends AppCompatActivity
{
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFacebookSdk();

        ...
    }
}