0

私のアプリでは、チャットに XMPP を使用しています。これでは、XMPP サーバーからデータベースにすべての連絡先をダウンロードするサービスを作成する必要があります。私は今、以下のコードのようにやっています.すべての連絡先を取得するには時間がかかります. このため、サービスを使用してこのジョブをバックグラウンドで実行し、DB に保存したいと考えています。データベースに新しい連絡先がある場合は、プロバイダーを使用して連絡先を更新します。

サービスの作成方法は知っていますが、ここでは名簿や XMPP 接続などのパラメーターをサービスに渡すことができません。これらのパラメーターは、XMPP サーバーから連絡先をダウンロードするために必要です。この問題を解決する方法を教えてください。これは私が今使っているコードです。

public class GmailXmppClient { 

   public GmailXmppClient(ChatAccountsFragment _fragment, Context _context) {

      this.fragment = _fragment;
      this.context = _context;


      ConnectionConfiguration config = new ConnectionConfiguration(server_host, SERVER_PORT, SERVICE_NAME);
      m_connection = new XMPPConnection(config);
      try {
          m_connection.connect();
      } catch (XMPPException e) {
           e.printStackTrace();
      }
  }

  public Roster getRoster() {
     Log.i(TAG, " getRoster ");
     return m_connection.getRoster();
  }

  public boolean Login(String uname, String pass ) throws XMPPException {

    m_connection.login(uname, pass);     
    this.fragment.Gtalk_logInComplete(uname, m_connection);             
    this.setPacketFilters();
    Presence presence = new Presence(Presence.Type.available);
    Log.i("ID", "" + presence);
    m_connection.sendPacket(presence);      
    return true;
  } 

  public void disconnect() {
     m_connection.disconnect();
  }
}

このコードの後の上記のコードから

this.fragment.Gtalk_logInComplete(uname, m_connection); 

このコードは、xmpp サーバーから連絡先を取得するために実行されます

private void getConts() {

   Roster roster = colors_xmpp_client.getRoster();
   String file_name;
   for (RosterEntry entry : roster.getEntries()) {
     if (entry.getType() == ItemType.to || entry.getType() == ItemType.both) {              

        boolean yes = Contact_data_source.checkUsername(entry.getUser());
        Log.i(TAG, "Con=" + yes);
        if (!yes) {
           String na = entry.getUser();
           String[] me = na.split("@");                     
           Bitmap buddy_img = buddyImage(entry, _connection);
           if (buddy_img != null)
               file_name = Store(buddy_img);
           else
               file_name = "";
           if (entry.getName() == null)
               Contact_data_source.createContact( entry.getUser(), entry.getUser(), Uname, file_name, UsedStrings.SipAccount, me[0] );
           else
               Contact_data_source.createContact( entry.getName(), entry.getUser(), Uname, file_name, UsedStrings.SipAccount, me[0] );
         } else {
          Log.i(TAG, "Con=exist");
         }
      }
    }               
 return null;
}
4

2 に答える 2

1

次のフローを使用できます
。1) アクティビティを開始し、RosterService をバインド します

。2) ContentObserver を目的のコンテキスト (アプリケーション コンテキストまたはアクティビティ) に登録します。


3) このコンテキストと contentObserver を RosterService に送信します

。 4) サービス中: 連絡先を取得して db に保存し、!!! >>

5) サービス中: context.getContentResolver().notifyChange(uriRosterChanged, contentObserver)

6) 次の連絡先に対して i.4 を繰り返します

i.5 -> contentObserver.onChange メソッドが起動されるため、ここで連絡先リストを更新できます


方法 2 サービスにパラメーターを送信する方法 2 方法について、エクストラと直接メソッド呼び出し ( setRosterNConnection() ) で説明:

アクティビティコード:

...
RosterService mService;
@Override public void onCreate(Bundle savedInstanceState) {
    ...
    Intent intent = new Intent(this, RosterService.class);
    intent.putExtra("Key", "Value");
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    ...
}
...
Roster mRoster ;
XMPPConnection mConnection;
...
private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className,
            IBinder service) {
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();       
        mService.setRosterNConnection(mRoster, mConnection);
        mService.doJob();
    }

    public void onServiceDisconnected(ComponentName arg0) {
                mService = null;
    }
};
...


RosterServiceコード:

// some class LocalBinder extends Binder{...} if some needs
private  LocalBinder mBinder = new LocalBinder(); // class LocalBinder extends Binder{...}
...
@Override
public IBinder onBind(Intent intent) {
    Bundle extras = intent.getExtras(); 
    if(extras == null)
        Log.d("RosterService","extras is empty");
    else
    {
        Log.d("RosterService","extras not empty");
        String key = (String) extras.get("Key");
        ...
    }
    return mBinder;
}
...
public void setRosterNConnection (Roster roster , XMPPConnection connection){
...
}
...
public void doJob(){
 // get and save contacts
    ...
}
于 2013-01-30T10:44:43.280 に答える
0

たぶん、AsyncTask でそれを行うことができます。

AsyncTask は別のスレッドで作業を行います。ユーザーがアクティビティを閉じると、asyncTask は停止します。

于 2013-01-30T09:44:22.083 に答える