首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从同步适配器访问应用程序对象

如何从同步适配器访问应用程序对象
EN

Stack Overflow用户
提问于 2014-06-17 13:46:25
回答 1查看 441关注 0票数 0

我正在使用开源服务器开发一个IM应用程序。我正在实现用于管理联系人的同步适配器。

如果访问应用程序类中保留的连接对象,则从同步适配器的onPerformSync()中返回null。

我做错了什么?我该怎么做?

我的申请课

代码语言:javascript
复制
public class MyApp extends Application {
    private Connection connection;
    private static MyApp instance;
    public static MyApp getInstance() {
    return instance;
}

public static void setInstance(MyApp instance) {
    MyApp.instance = instance;
}
@Override
public void onCreate() {
    super.onCreate();
    instance = new MyApp();
}
public Connection getAuthenticatedConnection() throws NullPointerException {

    if (instance.connection != null
            && instance.connection.isAuthenticated()) {
        return instance.connection;
    } else {

//  Calling service which will create connection and update the object

        Intent intent = new Intent(IM_Service_IntentMessaging.ACTION);
        intent = new Intent(getContext(), IM_Service_IntentMessaging.class);
        Bundle b = new Bundle();
        b.putInt("key", IM_Service_IntentMessaging.KEY_CONNECTION);
        intent.putExtras(b);
        this.context.startService(intent);

        throw new NullPointerException();
    }
//service calls this method to update the object
public void setConnection(Connection connection) {
    instance.connection = connection;
}
}

我的onPerformSync方法如下..。

代码语言:javascript
复制
@Override
public void onPerformSync(Account account, Bundle bundle, String authority,
        ContentProviderClient provider, SyncResult syncResult) {

    try {

        this.connection = MyApp.getInstance()
                .getAuthenticatedConnection();

        this.roster = this.connection.getRoster();

    } catch (NullPointerException e) {
                    //this line executes always
        Log.e(TAG, "connection null...ending....");

        return;
    }

    this.mAccount = account;

    Log.d(TAG, "...onPerformSync...");

    getAllContacts();
}

当我尝试在Application的getAuthenticatedConnection()方法中创建断点时,它没有被触发,但是我从那里调用来创建连接的服务IM_Service_IntentMessaging正在工作。

EN

回答 1

Stack Overflow用户

发布于 2014-06-18 05:13:58

我找到了解决问题的方法,我应该从同步适配器的清单文件声明中删除android:process=":sync"。正因为如此,它就像对待不同的过程一样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24265545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档