Android学习教程之悬浮窗菜单制作(9)
作者:天风隼 发布时间:2022-03-27 08:17:13
标签:Android,悬浮窗,菜单
本文实例为大家分享了Android悬浮窗菜单的具体代码,供大家参考,具体内容如下
MainActivity.java代码:
package siso.multilistview;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hideBothNavigationBarAndStatusBar();
setContentView(R.layout.activity_main);
FloatMenuManager.getInstance().startFloatView(this.getApplicationContext());
findViewById(R.id.hideStatuBarNaviBar).setOnClickListener(this);
}
private void hideBothNavigationBarAndStatusBar() {
View decorView = getWindow().getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
}
@Override
protected void onResume() {
super.onResume();
hideBothNavigationBarAndStatusBar();
FloatMenuManager.getInstance().showFloatingView();
}
@Override
protected void onPause() {
super.onPause();
FloatMenuManager.getInstance().hideFloatingView();
}
@Override
protected void onDestroy() {
super.onDestroy();
FloatMenuManager.getInstance().destroy();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.hideStatuBarNaviBar:
hideBothNavigationBarAndStatusBar();
break;
}
}
}
Const.java代码:
package siso.multilistview;
public interface Const {
String GAME_URL = "http://www.cnblogs.com/cate/html5/";
String HOME = "首页";
String FAVOUR = "收藏";
String FEEDBACK = "客服";
String MESSAGE = "消息";
String CLOSE = "关闭";
String[] MENU_ITEMS = {HOME, FAVOUR, FEEDBACK, MESSAGE, CLOSE};
}
FloatMenuManager.java代码:
package siso.multilistview;
import android.content.ComponentName;
import android.content.Context;
import android.os.IBinder;
import java.io.ObjectStreamException;
public class FloatMenuManager implements ServiceConnectionManager.QdServiceConnection {
private ServiceConnectionManager mServiceConnectionManager;
private FloatMenuManager() {
}
//静态内部类实现单例 优于双重检查锁(DCL)单例
public static FloatMenuManager getInstance() {
return FloatMenuHolder.single;
}
/**
* 静态内部类能够解决DCL双重检查锁失效的问题
*/
private static class FloatMenuHolder {
private static final FloatMenuManager single = new FloatMenuManager();
}
/**
* 防止反序列获取新的单例
*
* @return
* @throws ObjectStreamException
*/
private Object readResolve() throws ObjectStreamException {
return FloatMenuHolder.single;
}
private FloatMenuService mFloatViewService;
public void startFloatView(Context context) {
if (mFloatViewService != null) {
mFloatViewService.showFloat();
return;
}
if (mServiceConnectionManager == null) {
mServiceConnectionManager = new ServiceConnectionManager(context, FloatMenuService.class, this);
mServiceConnectionManager.bindToService();
}
}
/**
*/
public void addFloatMenuItem() {
if (mFloatViewService != null) {
}
}
/**
*
*/
public void removeMenuItem() {
if (mFloatViewService != null) {
}
}
/**
* 显示悬浮图标
*/
public void showFloatingView() {
if (mFloatViewService != null) {
mFloatViewService.showFloat();
}
}
/**
* 隐藏悬浮图标
*/
public void hideFloatingView() {
if (mFloatViewService != null) {
mFloatViewService.hideFloat();
}
}
/**
* 释放QDSDK数据
*/
public void destroy() {
if (mFloatViewService != null) {
mFloatViewService.hideFloat();
mFloatViewService.destroyFloat();
}
if (mServiceConnectionManager != null) {
mServiceConnectionManager.unbindFromService();
}
mFloatViewService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mFloatViewService = ((FloatMenuService.FloatMenuServiceBinder) service).getService();
if (mFloatViewService != null) {
mFloatViewService.showFloat();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
mFloatViewService = null;
}
}
FloatMenuService.java代码:
package siso.multilistview;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;
import com.yw.game.sclib.Sc;
import com.yw.game.sclib.ScCreateResultCallback;
import java.util.ArrayList;
import siso.floatmenu.FloatMenu;
import siso.floatmenu.MenuItem;
import siso.floatmenu.MenuItemView;
public class FloatMenuService extends Service implements View.OnClickListener {
private FloatMenu mFloatMenu;
private final static String TAG = FloatMenuService.class.getSimpleName();
private Handler mHandler = new Handler();
private int[] menuIcons = new int[]{R.drawable.yw_menu_account, R.drawable.yw_menu_favour, R.drawable.yw_menu_fb, R.drawable.yw_menu_msg, R.drawable.yw_menu_close};
@Override
public IBinder onBind(Intent intent) {
return new FloatMenuServiceBinder();
}
/**
* On create.
*/
@Override
public void onCreate() {
super.onCreate();
ArrayList<MenuItem> mMenuItems = new ArrayList<>();
for (int i = 0; i < menuIcons.length; i++) {
mMenuItems.add(new MenuItem(menuIcons[i], Const.MENU_ITEMS[i], android.R.color.black, this));
}
mFloatMenu = new FloatMenu.Builder(this).menuItems(mMenuItems).build();
mFloatMenu.show();
}
/**
* On click.
*
* @param v the v
*/
@Override
public void onClick(View v) {
if (v instanceof MenuItemView) {
MenuItemView menuItemView = (MenuItemView) v;
String menuItemLabel = menuItemView.getMenuItem().getLabel();
Toast.makeText(this, menuItemLabel, Toast.LENGTH_SHORT).show();
switch (menuItemLabel) {
case Const.HOME:
// TODO WHAT U WANT 此处模拟联网操作
mFloatMenu.startLoaderAnim();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.post(new Runnable() {
@Override
public void run() {
mFloatMenu.stopLoaderAnim();
goHomeIndex(FloatMenuService.this);
}
});
}
}).start();
break;
case Const.FAVOUR:
createSc();
break;
case Const.FEEDBACK:
break;
case Const.MESSAGE:
if (hasNewMsg) {
hasNewMsg = false;
} else {
hasNewMsg = true;
}
showRed();
break;
case Const.CLOSE:
hideFloat();
break;
}
}
}
private boolean hasNewMsg = false;
private void showRed() {
if (!hasNewMsg) {
mFloatMenu.changeLogo(R.drawable.yw_image_float_logo, R.drawable.yw_menu_msg, 3);
} else {
mFloatMenu.changeLogo(R.drawable.yw_image_float_logo_red, R.drawable.yw_menu_msg_red, 3);
}
}
private void createSc() {
//在service中的使用场景
PackageManager pm = this.getPackageManager();
ApplicationInfo appInfo = FloatMenuService.this.getApplicationInfo();
Drawable drawable = appInfo.loadIcon(pm);//当前app的logo
String name = appInfo.loadLabel(pm).toString();//当前app的名称
Intent intent = pm.getLaunchIntentForPackage(appInfo.packageName);//当前app的入口程序
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
new Sc.Builder(this, intent).
setName(name).
setAllowRepeat(true).
setIcon(drawable).
setCallBack(new ScCreateResultCallback() {
@Override
public void createSuccessed(String createdOrUpdate, Object tag) {
Toast.makeText(FloatMenuService.this, createdOrUpdate, Toast.LENGTH_SHORT).show();
}
@Override
public void createError(String errorMsg, Object tag) {
Toast.makeText(FloatMenuService.this, errorMsg, Toast.LENGTH_SHORT).show();
}
}).build().createSc();
}
/**
* Show float.
*/
public void showFloat() {
if (mFloatMenu != null)
mFloatMenu.show();
}
/**
* Hide float.
*/
public void hideFloat() {
if (mFloatMenu != null) {
mFloatMenu.hide();
}
}
/**
* Destroy float.
*/
public void destroyFloat() {
hideFloat();
if (mFloatMenu != null) {
mFloatMenu.destroy();
}
mFloatMenu = null;
}
/**
* On destroy.
*/
@Override
public void onDestroy() {
super.onDestroy();
destroyFloat();
}
public class FloatMenuServiceBinder extends Binder {
public FloatMenuService getService() {
return FloatMenuService.this;
}
}
private void goHomeIndex(Context context) {
Uri uri = Uri.parse(Const.GAME_URL);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
ServiceConnectionManager.java代码:
package siso.multilistview;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
public class ServiceConnectionManager implements ServiceConnection {
private final Context context;
private final Class<? extends Service> service;
private boolean attemptingToBind = false;
private boolean bound = false;
private QdServiceConnection mQdServiceConnection;
public ServiceConnectionManager(Context context, Class<? extends Service> service, QdServiceConnection mQdServiceConnection) {
this.context = context;
this.service = service;
this.mQdServiceConnection = mQdServiceConnection;
}
public void bindToService() {
if (!attemptingToBind) {
attemptingToBind = true;
context.bindService(new Intent(context, service), this, Context.BIND_AUTO_CREATE);
}
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
attemptingToBind = false;
bound = true;
mQdServiceConnection.onServiceConnected(componentName, iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mQdServiceConnection.onServiceDisconnected(componentName);
bound = false;
}
public void unbindFromService() {
attemptingToBind = false;
if (bound) {
context.unbindService(this);
bound = false;
}
}
public interface QdServiceConnection {
void onServiceConnected(ComponentName name, IBinder service);
void onServiceDisconnected(ComponentName name);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="通过服务启动浮动菜单"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>
<Button
android:id="@+id/hideStatuBarNaviBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:text="隐藏状态栏和导航栏"/>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="siso.multilistview">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- android:configChanges="keyboardHidden|orientation|screenSize"
防止横竖屏切换时重新执行oncreate-->
<activity android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".FloatMenuService"/>
</application>
</manifest>
Android Library Project(库项目)结构:
项目运行如图:


猜你喜欢
- 某少年宫引进了一批机器人小车。可以接受预先输入的指令,按指令行动。小车的基本动作很简单,只有3种:左转(记为L),右转(记为R),向前走若干
- <profiles> <profile> <
- 构建可重复读取inputStream的request我们知道,request的inputStream只能被读取一次,多次读取将报错,那么如何
- 最近用了Stream流,感觉超好用,记录一下。1、快速创建List比如我有个实体类User,User有个属性Namepublic class
- 最近项目中遇到一个问题,在用户没填数据的时候,我们需要接收从前端传过来的对象为null,但是前端说他们一个一个判断特别麻烦,只能传个空对象过
- 线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序
- 有兴趣的朋友可以回顾一下前两篇java并发编程专题(一)----线程基础知识java并发编程专题(二)----如何创建并运行java线程在现
- 场景点击第一个Activity中的按钮,启动第二个Activity,关闭第二个Activity,返回到第一个Activity。在第一个Act
- Java内部类一、 含义在Java编程语言里,程序是由类(class)构建而成的。在一个类的内部也可以声明类,我们把这样的类叫做内部类。二、
- 通过@Query注解支持JPA语句和原生SQL语句在SpringData中们可是使用继承接口直接按照规则写方法名即可完成查询的方法,不需要写
- 简介:Springboot使用Mybatis&Mybatis-plus 两者文件映射配置略有不同,之前我用的是Mybatis,但公司
- 前言最近在维护一个运营端的系统,和前端联调的过程中,经常需要排查一些交互上的问题,每次都得看前端代码的传参和后端代码的出参,于是打算给HTT
- 在程序开发中通常有推送消息的需求,通常为短信服务,邮件,电话提醒。短信及电话提醒通常需要向运营商购买服务调用接口,比较麻烦。邮件信息推送也是
- 在Google发布了support:design:23+以后我们发现有这么一个东西TextInputLayout,先看下效果图:<an
- 一、界面部分:首先,打开visual studio新建项目;然后使用“工具箱”添加控件:分别添加button,datagridview,te
- 在计算机科学中,正则表达式是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。在很多文本编辑器或其他工具里,正则表达式通常被
- 在web页面上我们可以通过frameset,iframe嵌套框架很容易实现各种导航+内容的布局界面,而在winform、WPF中实现其实也很
- QTableView是Qt中用来把数据集以表格形式提供给用户的一个控件QTableView类实现表格视图,QTableView的数据由继承Q
- 包的作用,1是为了防止类和方法的重名,2是为了管理众多的java类。步骤 1 工具包里面有很多个工具类之前讲了打印数据的方法:S
- 一、问题定义:问下有一个数组,这些数组中的值都有自己的权重,怎样设计才能高效的优先取出权重高的数??例如:权重: 8 2&nbs