欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android学习教程之悬浮窗菜单制作(9)

程序员文章站 2024-03-01 18:19:40
本文实例为大家分享了android悬浮窗菜单的具体代码,供大家参考,具体内容如下 mainactivity.java代码: package siso.mul...

本文实例为大家分享了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(库项目)结构:

Android学习教程之悬浮窗菜单制作(9)

项目运行如图:

Android学习教程之悬浮窗菜单制作(9)

Android学习教程之悬浮窗菜单制作(9)

Android学习教程之悬浮窗菜单制作(9)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。