I start a service class via <action android:name="android.intent.action.MAIN" />
. During onCreate()
, I want to make a call to getWindow()
but where can I get an activity reference?
Can you please help me in finding the reference?
I have used following to inflate a view which is the view that needs an activity reference to getWindow()
public class myServiceA extends Service {
public void onCreate() {
inflater1 = (LayoutInflater) this
.getSystemService(LAYOUT_INFLATER_SERVICE);
View1 = inflater1.inflate(R.layout.myview, null);
}
}
I want to loop through all the buttons to change the font size on the view so I need to call getWindow or is there anyway around this?
ViewGroup viewGroup = (ViewGroup) myActivityReference.getWindow()
.getDecorView();
Day 2: After some lengthy discussion, it seems there is no such activity for my service since it starts by loading the service directly. I have to change my question more specifics so that it solves what I want to do. Below is the code I use to format the font size of buttons on my other activity projects. Now, to do the same on the view inflated on my service class, it will lacks an activity reference. So how to modify the below code so it works for the view in the service class?
public void setButtonFontSize(Activity myActivityReference) {
ArrayList<Button> buttons = new ArrayList<Button>();
ViewGroup viewGroup = (ViewGroup) myActivityReference.getWindow()
.getDecorView();
findButtons(viewGroup, buttons, 1);
}
private static void findButtons(ViewGroup viewGroup,
ArrayList<Button> buttons, int mode) {
for (int i = 0, N = viewGroup.getChildCount(); i < N; i++) {
View child = viewGroup.getChildAt(i);
Button childButton;
if (child instanceof ViewGroup) {
findButtons((ViewGroup) child, buttons, mode);
} else if (child instanceof Button) {
buttons.add((Button) child);
childButton = (Button) child;
if (mode == 1)
childButton.setTextSize(12);
}
}
}
If your desire is to show your view on screen from inside the Service you could use the WindowManager:
You must also add the following permission to the manifest file: