简体   繁体   English

服务背景

[英]Service background

Code

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

Code

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

I have made the main layouts background as null but its still there... like how do i remove it... Meaning i have this service and when i start that service this activity will be inflated... But i want only the buttons to be inflated and not the whole activity... How do i achieve this......................................................................... 我已经将主要布局背景设置为null,但是仍然存在...就像我如何将其删除......意味着我拥有这项服务,当我启动该服务时,该活动将被夸大...但是我只想要按钮被夸大而不是整个活动...我该如何实现这一目标..................................... .......................................

Simply add this in your manifest. 只需在清单中添加即可。

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

In addition to this set 除了这套

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

to your root element 到你的根元素

Try this solution 试试这个解决方案

In manifest add this manifest添加此

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

Then in your launcher activity or any base activity add these permissions 然后在启动器活动或任何基本活动中添加这些权限

 private static final int CODE_DRAW_OVER_OTHER_APP_PERMISSION = 2084;

In onCreate() add this 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();
        }

Handle the request permission 处理请求权限

@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 
            }
        }
    }

Then Start your service and do this in service class , check the link below and do your stuff respectively https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064 然后启动您的service并在service类中执行此操作,检查下面的链接并分别进行操作https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064

Create a style in your styles.xml 在您的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>

Set it on your activity tag in manifest.xml 将其设置在manifest.xml的活动标签上

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

Don't set orientation on floating activity . 不要为浮动活动设置方向 As it will cause crash on Oreo devices. 因为这会导致Oreo设备崩溃。

Output 产量

产量

Update 更新

Start your activity with clear top stack from service. 从服务中清除顶部堆栈开始您的活动。

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

Update 2 更新2

If you want put any overlay on android screen, then put SYSTEM_ALERT_WINDOW in manifest. 如果要在Android屏幕上放置任何叠加层, SYSTEM_ALERT_WINDOW在清单中放入SYSTEM_ALERT_WINDOW

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM