本文实例为大家分享了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 {@OverrIDeprotected 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); } }@OverrIDeprotected voID onResume() { super.onResume(); hIDeBothNavigationBarandStatusbar(); floatMenuManager.getInstance().showfloatingVIEw(); }@OverrIDeprotected voID onPause() { super.onPause(); floatMenuManager.getInstance().hIDefloatingVIEw(); }@OverrIDeprotected voID onDestroy() { super.onDestroy(); floatMenuManager.getInstance().destroy(); }@OverrIDepublic 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,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,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)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)