大佬教程收集整理的这篇文章主要介绍了Android学习教程之悬浮窗菜单制作(9),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例为大家分享了Android悬浮窗菜单的具体代码,供大家参考,具体内容如下
@H_283_0@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; } }
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://scheR_2_11845@as.android.com/apk/res/android" xmlns:tools="http://scheR_2_11845@as.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://scheR_2_11845@as.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)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。