在我的活动中,我不断轮询服务器以获取更新,并将该数据存储在数据库中并从那里显示。我的问题是,当我获得更新时,如何在不刷新的情况下显示在该活动屏幕上(就像 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 类型的通知。