在我的活動中,我繼續輪詢服務器以獲取更新,並將該數據存儲在數據庫中,並從那裏顯示出來。 我的問題是當我得到更新,然後如何顯示在該活動屏幕上,而不刷新它(只是像FACEBOOK一樣) 請幫我。如何在android代碼中獲取像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類型的通知。
在'Activity'中使用私有'BroadcastReceiver'來更新你的UI!嘗試使用關鍵字搜索谷歌:「如何更新服務中的活動」! –