Getting window display from service android

Sunny picture Sunny · Sep 29, 2012 · Viewed 10.8k times · Source

I am trying to display a window from service but don't getting it how to do this

Here is my code--

In onStart method of Service

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.main1, null);
    WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.type = 2002;
    params.format = 1;
    params.flags = 40;
    params.height = WindowManager.LayoutParams.FILL_PARENT;
    params.width = WindowManager.LayoutParams.FILL_PARENT;
    manager.addView(view, params);

     /** just a timer *****/
    textView = (TextView) view.findViewById(R.id.textView1);
    final Timer t = new Timer("hello", true);
    t.schedule(new TimerTask() {

    @Override
    public void run() {
        textView.post(new Runnable() {

        public void run() {
            seconds++;
            if (seconds == 60) {
            seconds = 0;
            minute++;
            }
            if (minute == 60) {
            minute = 0;
            hour++;
            }
            textView.setText(""
                + (hour > 9 ? hour : ("0" + hour)) + " : "
                + (minute > 9 ? minute : ("0" + minute))
                + " : "
                + (seconds > 9 ? seconds : "0" + seconds));

        }
        });

    }
    }, 1000, 1000);

    view.findViewById(R.id.stop).setOnClickListener(
        new OnClickListener() {

        public void onClick(View v) {
            t.cancel();
        }
        });

But nothing shown up. How can i display my views in window from service?

Answer

Sunny picture Sunny · Sep 29, 2012

Wooohoooo Got it work The trick was just using system window service

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.FILL_PARENT, 150,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.CENTER;

add the permission

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