服务背景

时间:2018-09-17 10:20:20

标签: java android xml service

代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@null"
android:id="@+id/relativelayout"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<Button
    android:id="@+id/button5"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_centerVertical="true"
    android:layout_marginEnd="9dp"
    android:layout_toStartOf="@+id/button12"
    android:background="@drawable/service_button_background"
    android:text="Akash" />

<Button
    android:id="@+id/button6"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_above="@+id/button5"
    android:layout_alignStart="@+id/button7"
    android:background="@drawable/service_button_background"
    android:text="Sanjana" />

<Button
    android:id="@+id/button7"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignStart="@+id/button5"
    android:layout_below="@+id/button5"
    android:layout_marginStart="19dp"
    android:background="@drawable/service_button_background"
    android:text="Sahana" />

<Button
    android:id="@+id/button12"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_above="@+id/button5"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="30dp"
    android:background="@drawable/service_button_background"
    android:text="Akash" />

<Button
    android:layout_width="70dp"
    android:background="@drawable/service_button_background"
    android:text="Akash"
    android:layout_height="70dp"
    android:layout_alignParentEnd="true"
    android:layout_alignTop="@+id/button7"
    android:layout_marginTop="30dp"
    android:id="@+id/button11" />

代码

public class FloatingWindow extends Service{
WindowManager wm;
RelativeLayout ll;
LayoutInflater li;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    final View myview;
    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);

    /////////////////////////Another params


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

    params.gravity = Gravity.RIGHT | Gravity.CENTER;
    myview = li.inflate(R.layout.service_pie, null); // your layout here

    wm.addView(myview, params);
    params.x = 0;
    params.y = 0

}

@Override
public void onDestroy() {
    super.onDestroy();
    stopSelf();
}

我已经将主要布局背景设置为null,但是仍然存在...就像我如何删除它...意味着我有这项服务,当我启动该服务时,该活动将被夸大...但是我想要只有要充气的按钮,而不是整个活动...我如何实现这一目标...................... ..........................................

3 个答案:

答案 0 :(得分:1)

只需将此添加到清单中即可。

<activity
    android:name=".YOUR_ACTIVITY"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

除了这套

android:background="@android:color/transparent" 

到您的根元素

答案 1 :(得分:1)

尝试此解决方案

manifest中添加

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

然后在启动器活动或任何基本活动中添加这些权限

 private static final int CODE_DRAW_OVER_OTHER_APP_PERMISSION = 2084;

onCreate()中添加

if (SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
            //finish();
        }

处理请求权限

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CODE_DRAW_OVER_OTHER_APP_PERMISSION) {
            if (SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
            } else {
               // do stuff here 
            }
        }
    }

然后启动service并在service类中进行此操作,检查下面的链接并分别进行操作 https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064

答案 2 :(得分:0)

在您的styles.xml

中创建样式
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

manifest.xml的活动标签上设置

<activity android:name=".YourActivity" android:theme="@style/Theme.Transparent">
...
</activity>

不要为浮动活动设置方向。因为它将导致Oreo设备崩溃。

输出

output

更新

从服务的顶部清除堆栈开始活动。

    Intent intent1 = new Intent(this, MainActivity.class);
    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent1);

更新2

如果要在Android屏幕上放置任何叠加层,请在清单中放置SYSTEM_ALERT_WINDOW

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