Android學(xué)習(xí)教程之懸浮窗菜單制作(9)
本文實例為大家分享了Android懸浮窗菜單的具體代碼,供大家參考,具體內(nèi)容如下
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 = "關(guān)閉";
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() {
}
//靜態(tài)內(nèi)部類實現(xiàn)單例 優(yōu)于雙重檢查鎖(DCL)單例
public static FloatMenuManager getInstance() {
return FloatMenuHolder.single;
}
/**
* 靜態(tài)內(nèi)部類能夠解決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) {
}
}
/**
* 顯示懸浮圖標(biāo)
*/
public void showFloatingView() {
if (mFloatViewService != null) {
mFloatViewService.showFloat();
}
}
/**
* 隱藏懸浮圖標(biāo)
*/
public void hideFloatingView() {
if (mFloatViewService != null) {
mFloatViewService.hideFloat();
}
}
/**
* 釋放QDSDK數(shù)據(jù)
*/
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 此處模擬聯(lián)網(wǎng)操作
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);//當(dāng)前app的logo
String name = appInfo.loadLabel(pm).toString();//當(dāng)前app的名稱
Intent intent = pm.getLaunchIntentForPackage(appInfo.packageName);//當(dāng)前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="通過服務(wù)啟動浮動菜單"
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="隱藏狀態(tài)欄和導(dǎo)航欄"/>
</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"
防止橫豎屏切換時重新執(zhí)行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(庫項目)結(jié)構(gòu):

項目運行如圖:



以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)可播放GIF動畫的ImageView
這篇文章主要為大家詳細介紹了Android實現(xiàn)可播放GIF動畫的ImageView,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android 使用SharePerference判斷是否為第一次登陸的實現(xiàn)代碼
很多app中在第一次安裝登陸時會有引導(dǎo)歡迎界面,第二次打開時就不再顯示引導(dǎo)頁面。這個怎么實現(xiàn)呢?下面小編給大家介紹下使用SharePerference判斷是否為第一次登陸的實現(xiàn)代碼,需要的的朋友參考下吧2017-03-03
Android?Studio?Electric?Eel支持手機投屏
這篇文章主要為大家介紹了Android?Studio?Electric?Eel支持手機投屏功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
Android編程實現(xiàn)的重力感應(yīng)示例代碼
這篇文章主要介紹了Android編程實現(xiàn)的重力感應(yīng)效果,以完整示例代碼形式分析了重力感應(yīng)的原理與實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android DrawLayout結(jié)合ListView用法實例
這篇文章主要介紹了Android DrawLayout結(jié)合ListView用法實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09

