Accessing findViewById from Service

2019-07-24 17:12发布

I want to Access findViewById() from an Service without passing it as an parameter or intent in the constructor of the service, Is that possible?

Her my code:

public class ManageDataService extends Service implements LocationListener {
    public void onCreate() {
        TextView longitudeField = (TextView) findViewById(R.id.overview_position_longitude);
    }
}

I have to get the Activity's context somehow, right?

I didn't find any matching solutions on the internet.

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-24 17:46

It's really bad idea to update UI form service. I think good approach to to it is usage of BroadcastReceivers data Observer. Just register one for your activity and pass value to update UI.

查看更多
登录 后发表回答