2013-01-04 43 views
0

在我的活動中,我繼續輪詢服務器以獲取更新,並將該數據存儲在數據庫中,並從那裏顯示出來。 我的問題是當我得到更新,然後如何顯示在該活動屏幕上,而不刷新它(只是像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類型的通知。

+0

在'Activity'中使用私有'BroadcastReceiver'來更新你的UI!嘗試使用關鍵字搜索谷歌:「如何更新服務中的活動」! –

回答

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. 綁定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); 
    } 
} 

現在,每當被執行的報警,如果有綁定到該服務的任何活動比較(意味着如果用戶有應用程序開啓),該活動將得到更新。現在實現你想要處理更新的任何代碼。

我真的建議你閱讀Bound Services in Android Developers。這就是我學會綁定到服務的地方,我的代碼深受該鏈接的影響。

+0

感謝您的幫助@Niklas。我仍然需要嘗試這個。 –

+0

不客氣!如果你也需要我,我可以詳細說明每一步的描述:) –

+0

我已經提到了下面正在處理的代碼。如果你可以看看它,請請。 –

相關問題