I am working on an IM application with open fire server. I'm implementing Sync Adapter for managing the contacts.
From my sync adapter's onPerformSync()
if I access the connection object which I kept in my Application class it returns null.
What am I doing wrong? How should I do that?
My Application class
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;
}
}
And my onPerformSync method is as follows...
@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();
}
When I tried with creating break point in Application's getAuthenticatedConnection()
method it didn't get triggered but the service IM_Service_IntentMessaging
which i called from there to create connection is working.