Getting window display from service android

僤鯓⒐⒋嵵緔 提交于 2019-12-30 06:33:53

问题


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?


回答1:


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"/>



回答2:


Key handling can also be implemented in service !! Generally if WindowManager is used to display view in service it does'nt take any key events most of the time !!

But you can make an interface with 'dispatchKeyEvent' or any other key handling methods(onKeyUp, onKeyDown etc.) and implement them is service.

Happy coding.



来源:https://stackoverflow.com/questions/12650463/getting-window-display-from-service-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!