2012-06-08 65 views
0

我有這個問題,我正在爲清單中的AlarmManager註冊我的廣播接收器。我正在通過待定Intent在我的活動中安排它。從清單中註冊的廣播接收器更新UI

AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
    PendingIntent pendingIntent =PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReciever.class),PendingIntent.FLAG_CANCEL_CURRENT); 
    alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, Constants.ALARM_TRIGGER_AT_TIME,Constants.ALARM_INTERVAL, pendingIntent); 

但在我的接收器類中,我正在使用AlarmManager ...進行一些網絡更新,我將其保存在數據庫中。我在活動中有一個ListAdapter,需要notifydatasetChanged ...爲此我需要一個活動實例。

如何得到它?基本上我想至少在我的應用程序可見時更新我的​​用戶界面。

+0

您已經通過AlarmReciever.class作爲實例使用它。 –

+0

不,我想要我的廣播reciever類的活動的實例..其實我嘗試使用內部類的活動本身,但它不工作...我認爲清單不支持內部類路徑或什麼? – sheetal

回答

0

我得到了answer..For刷新我的活動是可見的後意圖

 Intent i=new Intent(caller,Dot.class); 
     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     caller.startActivity(i); 

如果活動可見它會調用OnNewIntent()在你的代碼,因此的onResume(),您可以更新你的UI ..

2

創建活動爲您提供BroadcastReceiver,它註冊您的活動的onResume內註銷其在。之後完成網絡升級,創建要發送給你的活動和火災的Intent,把所有數據如果你的活動是在前臺,那麼只有它會收到這個意圖。從你的活動中的BroadcaseReceiveronReceive(),你可以得到所有的數據,然後更新Ui。

創建BroadcastReceiver您的活動::

private class MyReceiver extends BroadcastReceiver 
{ 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      /////// update your UI from here ////////  
      } 

} 

裏面onCreate(),從OnResume()創建該接收機::

myReceiver=new MyReceiver(); 

註冊此接收器的實例::

IntentFilter filter=new IntentFilter(); 
    filter.setPriority(1); 
    filter.addAction("MyPackageName.MyAction"); 
    registerReceiver(myReceiver, filter); 

重寫它在現在::

unregisterReceiver(myReceiver); 

,消防完成網絡更新火災的意圖開始這個接收器::

  Intent broadcastIntent=new Intent(); 
      broadcastIntent.setAction("MyPackageName.MyAction"); 
      broadcastIntent.putExtra();// put data to send to your activity 
      sendBroadcast(broadcastIntent); 
+0

我不知道爲什麼,但註冊通過一個活動的廣播onRecieve沒有得到稱自己....你可以幫助我...我需要做別的? – sheetal

+0

@sheetal,在filter.setAction()中設置的動作,以及在broadcastIntent.setAction中激發意圖時使用的動作必須匹配,並且動作應該爲表單packageName.MyAction.eg::filter.addAction("com.ap.myApp .myAction「); – Eight

+0

任何想法如何更新現有的用戶界面屏幕...只是想更新接收到的廣播屏幕上的狀態。 – CoDe

相關問題