how to loop through all buttons on a view inflated in an android service class?

337 views Asked by At

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);
        }
    }
}
2

There are 2 answers

3
Cristian Neagu On

If your desire is to show your view on screen from inside the Service you could use the WindowManager:

public class myServiceA extends Service {
    public void onCreate() { 
        Inflater inflater = LayoutInflater.from(this); 

        View view1 = inflater.inflate(R.layout.myview, null);

        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;

        windowManager.addView(view1, params);
    } 
}

You must also add the following permission to the manifest file:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
0
user2911996 On

I got the answer after changing the specifics of my problem:

 ViewGroup viewGroup = (ViewGroup) View1;