首页

Android悬浮窗示例(floatingwindow)

java

2020-6-21

悬浮效果如下,本实例中悬浮的是图片、按钮、视频,其它元素同理

package dongzhong.testforfloatingwindow;

import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (!Settings.canDrawOverlays(this)) {
                Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
                startService(new Intent(MainActivity.this, FloatingButtonService.class));
            }
        } else if (requestCode == 1) {
            if (!Settings.canDrawOverlays(this)) {
                Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
                startService(new Intent(MainActivity.this, FloatingImageDisplayService.class));
            }
        } else if (requestCode == 2) {
            if (!Settings.canDrawOverlays(this)) {
                Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
                startService(new Intent(MainActivity.this, FloatingVideoService.class));
            }
        }
    }

    public void startFloatingButtonService(View view) {
        if (FloatingButtonService.isStarted) {
            return;
        }
        if (!Settings.canDrawOverlays(this)) {
            Toast.makeText(this, "当前无权限,请授权", Toast.LENGTH_SHORT);
            startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:"   getPackageName())), 0);
        } else {
            startService(new Intent(MainActivity.this, FloatingButtonService.class));
        }
    }

    public void startFloatingImageDisplayService(View view) {
        if (FloatingImageDisplayService.isStarted) {
            return;
        }
        if (!Settings.canDrawOverlays(this)) {
            Toast.makeText(this, "当前无权限,请授权", Toast.LENGTH_SHORT);
            startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:"   getPackageName())), 1);
        } else {
            startService(new Intent(MainActivity.this, FloatingImageDisplayService.class));
        }
    }

    public void startFloatingVideoService(View view) {
        if (FloatingVideoService.isStarted) {
            return;
        }
        if (!Settings.canDrawOverlays(this)) {
            Toast.makeText(this, "当前无权限,请授权", Toast.LENGTH_SHORT);
            startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:"   getPackageName())), 2);
        } else {
            startService(new Intent(MainActivity.this, FloatingVideoService.class));
        }
    }
}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
TestForFloatingWindow-master/.gitignore , 124
TestForFloatingWindow-master/app/.gitignore , 7
TestForFloatingWindow-master/app/build.gradle , 936
TestForFloatingWindow-master/app/proguard-rules.pro , 751
TestForFloatingWindow-master/app/src/androidTest/java/dongzhong/testforfloatingwindow/ExampleInstrumentedTest.java , 763
TestForFloatingWindow-master/app/src/main/AndroidManifest.xml , 1074
TestForFloatingWindow-master/app/src/main/java/dongzhong/testforfloatingwindow/FloatingButtonService.java , 3281
TestForFloatingWindow-master/app/src/main/java/dongzhong/testforfloatingwindow/FloatingImageDisplayService.java , 4602
TestForFloatingWindow-master/app/src/main/java/dongzhong/testforfloatingwindow/FloatingVideoService.java , 4936
TestForFloatingWindow-master/app/src/main/java/dongzhong/testforfloatingwindow/MainActivity.java , 3310
TestForFloatingWindow-master/app/src/main/res/drawable-v24/ic_launcher_foreground.xml , 1880
TestForFloatingWindow-master/app/src/main/res/drawable/ic_launcher_background.xml , 5606
TestForFloatingWindow-master/app/src/main/res/drawable/image_01.jpg , 69160
TestForFloatingWindow-master/app/src/main/res/drawable/image_02.jpg , 219542
TestForFloatingWindow-master/app/src/main/res/drawable/image_03.jpg , 142166
TestForFloatingWindow-master/app/src/main/res/drawable/image_04.jpg , 299478
TestForFloatingWindow-master/app/src/main/res/drawable/image_05.jpg , 102561
TestForFloatingWindow-master/app/src/main/res/layout/activity_main.xml , 874
TestForFloatingWindow-master/app/src/main/res/layout/image_display.xml , 408
TestForFloatingWindow-master/app/src/main/res/layout/video_display.xml , 376
TestForFloatingWindow-master/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml , 272
TestForFloatingWindow-master/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml , 272
TestForFloatingWindow-master/app/src/main/res/mipmap-hdpi/ic_launcher.png , 3056
TestForFloatingWindow-master/app/src/main/res/mipmap-hdpi/ic_launcher_round.png , 5024
TestForFloatingWindow-master/app/src/main/res/mipmap-mdpi/ic_launcher.png , 2096
TestForFloatingWindow-master/app/src/main/res/mipmap-mdpi/ic_launcher_round.png , 2858
TestForFloatingWindow-master/app/src/main/res/mipmap-xhdpi/ic_launcher.png , 4569
TestForFloatingWindow-master/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png , 7098
TestForFloatingWindow-master/app/src/main/res/mipmap-xxhdpi/ic_launcher.png , 6464
TestForFloatingWindow-master/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png , 10676
TestForFloatingWindow-master/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png , 9250
TestForFloatingWindow-master/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png , 15523
TestForFloatingWindow-master/app/src/main/res/values/colors.xml , 208
TestForFloatingWindow-master/app/src/main/res/values/strings.xml , 84
TestForFloatingWindow-master/app/src/main/res/values/styles.xml , 383
TestForFloatingWindow-master/app/src/test/java/dongzhong/testforfloatingwindow/ExampleUnitTest.java , 409
TestForFloatingWindow-master/build.gradle , 589
TestForFloatingWindow-master/gradle.properties , 730
TestForFloatingWindow-master/gradle/wrapper/gradle-wrapper.jar , 53636
TestForFloatingWindow-master/gradle/wrapper/gradle-wrapper.properties , 230
TestForFloatingWindow-master/gradlew , 4971
TestForFloatingWindow-master/gradlew.bat , 2314
TestForFloatingWindow-master/settings.gradle , 15
没有账号? 忘记密码?

社交账号快速登录