17

I want to inflate an xml layout in a service. How do you do this? I tried to use inflater and Window Manager but I think it don't work. Any tactics on this? Thanks.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main"
>
    <LinearLayout
        android:id="@+id/top"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        >


    <FrameLayout
        android:id="@+id/topLeft"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:tag="topLeft"
        android:layout_weight="1.0"
        android:background="#ff3d38"></FrameLayout>


    <FrameLayout
        android:id="@+id/topRight"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:tag="topRight"

    android:layout_weight="1.0"
        android:background="#fff839"></FrameLayout>

    </LinearLayout>

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:id="@+id/bottom"
    >

<FrameLayout
        android:id="@+id/bottomLeft"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_weight="1.0"
        android:background="#1e22ff">

    </FrameLayout>

    <FrameLayout
        android:id="@+id/bottomRight"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_weight="1.0"
        android:background="#32ff1f"></FrameLayout>

This is my Service Code for the xml to be displayed.

package com.toksis.pvscreen;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;


public class DragDropButtonMainService extends Service {
private LinearLayout main;
private LinearLayout firstLayer;
private LinearLayout secondLayer;
private FrameLayout  vTopLeft;
private FrameLayout  vTopRight;
private FrameLayout  vBottomLeft;
private FrameLayout  vBottomRight;



private Button mainButton;
private ViewGroup mView;
private LayoutInflater inflater;

public IBinder onBind(Intent intent) {
    return null;
}


public void onCreate() {
    super.onCreate();

    createDragDropLayout();



}



void createDragDropLayout(){

   mainButton = new Button(this);
   mainButton.setText("Main");


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




   WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

   inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   mView =  (ViewGroup) inflater.inflate(R.layout.dragdroplayout, null);


   vTopLeft   = (FrameLayout) mView.findViewById(R.id.topLeft);
   vTopRight  = (FrameLayout)  mView.findViewById(R.id.topRight);
   vBottomLeft = (FrameLayout) mView.findViewById(R.id.bottomLeft);
   vBottomRight= (FrameLayout) mView.findViewById(R.id.bottomRight);
   firstLayer = (LinearLayout) mView.findViewById(R.id.top);
   secondLayer = (LinearLayout) mView.findViewById(R.id.bottom);

   //    main = (LinearLayout) mView.findViewById(R.id.main);

  //        main.addView(firstLayer);
  //      main.addView(secondLayer);


 //    firstLayer.addView(vTopLeft);
 //           firstLayer.addView(vTopRight);

 //         secondLayer.addView(vBottomLeft);
//       secondLayer.addView(vBottomRight);

  // wm.addView(mainButton, params);
     wm.addView(mView,params);

   Log.d("tok", "add mview");


   }
}
The One
  • 1,085
  • 6
  • 13
  • 25
  • No UI for Service. Not Possible. What you want to do? – kumar_android Oct 23 '13 at 11:11
  • I'm having the same broblem ,did you solve it ? i mean finaly it's possible to have graphical layout in a service ? @toksis – Amina Apr 20 '14 at 18:52
  • @The One, did you solve the problem? Do you maybe know how receive home button press, volume button pres...etc? – 5er Nov 03 '15 at 22:08

1 Answers1

23

it is possible to place a graphic on the service the code is this:

li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        wm = (WindowManager) getSystemService(WINDOW_SERVICE);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                //WindowManager.LayoutParams.TYPE_INPUT_METHOD |
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,// | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.RIGHT | Gravity.TOP;
        myview = li.inflate(R.layout.traslucent, null);     

        wm.addView(myview, params);
Mayank
  • 1,364
  • 1
  • 15
  • 29
  • What permission are you talking @Mayank ? – The One Oct 23 '13 at 11:17
  • is your myview a ViewGroup? – The One Oct 23 '13 at 11:27
  • Why is there a myview.setOnTouchListener(this);? I removed it and now it is working... Please edit it? then I will tag is as correct. – The One Oct 23 '13 at 12:12
  • And also, is it possible to add a child button? I want to add it on inside the FrameLayout... – The One Oct 23 '13 at 12:18
  • Adding a child to the Framelayout by using Addview. Thanks, I will now add tag this as checked. – The One Oct 23 '13 at 12:24
  • this is just a part of my code that i did a long time back. myview is view and you can delete setontouchlistener line. you can add a button but it wont respond to touch( as far as i can think of). – Mayank Oct 23 '13 at 13:00
  • @toksis if you get a method to make your button touchable do let me know. – Mayank Oct 23 '13 at 13:24
  • and i solved the button crisis as well try setting type to TYPE_SYSTEM_ALERT and now add permission that i mentioned in my unedited answer. – Mayank Oct 23 '13 at 18:59
  • "The world is too big mom" "then make it small" havent you seen superman. – Mayank Oct 24 '13 at 17:15
  • @Mayank, do you maybe know how receive touch event and home button pressed in sevice and volume up...? – 5er Nov 03 '15 at 22:06
  • @5er I could not understand your question. – Mayank Nov 04 '15 at 07:17
  • @Mayank, I am trying to catch touch events on on my service. I inflow view similar as your suggestion was. I intercept touch event, and key events, so I can prevent user to iterate with underly Activit. Now the only problem I have is notificationBar. the user can still interact with it. – 5er Nov 04 '15 at 21:51