0

私のアクティビティでは、サーバーをポーリングして更新を取得し続け、そのデータをデータベースに保存し、そこから表示します。私の質問は、更新を取得したときに、更新せずにそのアクティビティ画面に表示する方法です (FACEBOOK のように) 助けてください。


ログイン アクティビティでは、この関数を呼び出します。

    public void PollingViaAlarm() {
try {
        Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(System.currentTimeMillis());
 Intent intent = new Intent(getApplicationContext(),PollingAlarm.class);
 intent.putExtra("title", "Polling Via Server");
 PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,  intent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + 5 * 60  * 1000, 5 * 60 * 1000,pendingIntent);
} catch (Exception e) {

}}

PollingAlarm クラスでは、関数を呼び出してサーバーをポーリングします。

 cnstnt.getUserMessagesPollingAlarm(patient_id,username,password, "123");
 cntxt.startService(new Intent(cntxt, NotificationService.class));

通知クラスでは、

 package com.vcare.cnstnt;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
 import android.util.Log;

public class NotificationService extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    SharedPref shrd = new SharedPref(getApplicationContext());
    Intent i = new Intent("android.intent.action.MAIN").putExtra(
            "msg_count", "" + shrd.getMsgCount());
    Log.e("msg_count in notification service", "" + shrd.getMsgCount());
    if (shrd.getMsgCount() > 0) {
        Log.e("starting broadcasting","");
        this.sendBroadcast(i);
        this.stopSelf();
    }
}

 }

可能であれば、私がやろうとしていることをチェックしていただけますか。Facebookのような通知を実現する必要があります。

4

1 に答える 1

1
  1. を使用しBroadcastRecieverて開始しService、サーバーをポーリングします。
  2. Serviceようなものと呼ばれるインターフェースと関数を作成します

    public void setOnListener(Listener listener) {...}

  3. そのインターフェイスをActivityおよびオーバーライド関数に実装します。

  4. インターフェイスにバインドするには:

    private ServiceConnection mConnection = new ServiceConnection() {
    
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            final MyService mBoundService = ((MyService.LocalBinder) service)
                    .getService();
            mBoundService.setOnListener(MyActivity.this);
        }
    
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    
    };
    
  5. をにバインドActivityしますService

    @Override
    public void onStart() {
        super.onStart();
        final Intent intent = new Intent(this, MyService.class);
        bindService(intent, mConnection, 0);
    }
    
  6. Serviceこれで、サーバーの更新がダウンロードされるたびに、インターフェイス関数を呼び出すだけです。

  7. 同様にバインドを解除することを確認してくださいService

    @Override
    public void onStop() {
        super.onStop();
        unbindService(mConnection);
    }
    

ノート!以下のコードはテストしていません。

更新されたコードへの応答:アラームは問題ないように見えますが、サービスにまったく拘束されていません。サービスにバインドできるようにするには、そのサービスは次のようになっている必要があります。

public class NotificationService extends Service {

    private final IBinder mBinder = new LocalBinder();
    private NotificationListener listener;

    public void setListener(NotificationListener listener) {
        this.listener = listener;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        SharedPref shrd = new SharedPref(getApplicationContext());
        Log.e("msg_count in notification service", "" + shrd.getMsgCount());
        if (listener != null && shrd.getMsgCount() > 0) {
            Log.e("starting broadcasting", "");
            listener.onMessageRecieved(shrd.getMsgCount());
        }
        this.stopSelf();
    }

    public interface NotificationListener {
        public void onMessageRecieved(int messageCount);
    }

    public class LocalBinder extends Binder {
        public NotificationService getService() {
            return NotificationService.this;
        }
    }
}

更新を受信する必要があるすべてのアクティビティは、を実装する必要がありNotificationListenerます。次に、サービスにバインドし、サービスをアクティビティに接続するプロセスで、リスナーをバインドします。このように見えるはずです。

public class TestActivity extends Activity implements NotificationService.NotificationListener {

    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            final NotificationService mBoundService = ((NotificationService.LocalBinder) service).getService();
            mBoundService.setListener(TestActivity.this);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }

    };

    @Override
    public void onMessageRecieved(int messageCount) {
        // DO WHATEVER YOU'D LIKE HERE,
        // LIKE UPDATE YOUR UI!
    }

    @Override
    public void onResume() {
        super.onResume();
        // Bind service
        final Intent serviceIntent = new Intent(this, NotificationService.class);
        // 0 means do not create service if it doesnt exist
        bindService(serviceIntent, mConnection, 0);
    }

    @Override
    public void onPause() {
        super.onPause();
        unbindService(mConnection);
    }
}

これで、そのアラームが実行されるたびに、サービスにバインドされたアクティビティがある場合(つまり、ユーザーがアプリを開いている場合)、そのアクティビティは更新を受け取ります。次に、更新を処理するコードを実装します。

Androidデベロッパーのバウンドサービスについてお読みになることをお勧めします。そこで私はサービスにバインドすることを学びました。私のコードはそのリンクの影響を強く受けています。

于 2013-01-04T16:29:33.447 に答える