Updating Textview in an Activity with Local Service

前端 未结 2 1960
我寻月下人不归
我寻月下人不归 2021-01-05 14:26

I have an Activity with R.id.eventDistance and R.id.eventTime to display the distance & travel time based on current location. I calculate these values every 30 seconds

相关标签:
2条回答
  • 2021-01-05 14:43

    please see this flow.

    1.in your LocationService class when you want to update UI. in your case it should be called every 30 seconds.

    Intent i = new Intent("LOCATION_UPDATED");
    i.putExtra("<Key>","text");
    
    sendBroadcast(i);
    

    2.in your UI (HomeActivity) class

    in onCreate()

    registerReceiver(uiUpdated, new IntentFilter("LOCATION_UPDATED"));
    

    in class

    private BroadcastReceiver uiUpdated= new BroadcastReceiver() {
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            TextView.setText(intent.getExtras().getString("<KEY>"))
    
        }
    };
    

    in onDestroy()

    unregisterReceiver(uiUpdated);
    
    0 讨论(0)
  • 2021-01-05 14:53

    You should bind your Activity to your service so that they can directly communicate and message pass.

    Ref

    http://developer.android.com/guide/components/bound-services.html

    0 讨论(0)
提交回复
热议问题