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. 使用 aBroadcastReciever并启动 aService来轮询服务器。
  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. 绑定ActivityService

    @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 Developers 中的绑定服务。那是我学会绑定到服务的地方,我的代码深受该链接的影响。

于 2013-01-04T16:29:33.447 回答