Accessing findViewById from Service

2019-07-24 17:11发布

问题:

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:

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.