Android Learning Course Suspension Window menu making (9) _android

Source: Internet
Author: User
Tags static class

This example for you to share the Android Suspension Window menu of the specific code for your reference, the specific contents are as follows

Mainactivity.java Code:

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 (B
    Undle 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:hidebothnavigat
    Ionbarandstatusbar ();
    Break

 }
    }
    }

Const.java Code:

Package Siso.multilistview;

Public interface Const {
  String game_url = "http://www.cnblogs.com/cate/html5/";
  String home = "homepage";
  String favour = "collection";
  String FEEDBACK = "Customer service";
  String message = "Messages";
  String close = "Off";

  String[] Menu_items = {home, favour, FEEDBACK, message, close};
}

Floatmenumanager.java Code:

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 () {}//static inner class implementations are better than double check Locks (DCL) Single case public static Floatmenumanager getinstance () {retur
  n Floatmenuholder.single; /** * Static internal class can solve the problem of DCL double check lock failure * * private static class Floatmenuholder {private static final Floatmenumana
  Ger single = new Floatmenumanager ();  /** * Prevents the reverse sequence from acquiring a new single instance * * @return * @throws objectstreamexception/private Object readresolve () throws
  objectstreamexception {return floatmenuholder.single;

  Private Floatmenuservice Mfloatviewservice;
      public void Startfloatview {if (mfloatviewservice!= null) {mfloatviewservice.showfloat (); Return; } if (Mserviceconnectionmanager = null) {Mserviceconnectionmanager = new Serviceconnectionmanager (context, Flo
      Atmenuservice.class, this);
    Mserviceconnectionmanager.bindtoservice ();
  }/**/public void Addfloatmenuitem () {if (Mfloatviewservice!= null) {}}/** * * * public void Removemenuitem () {if (Mfloatviewservice!= null) {}}/** * Display suspension icon */public void SHOWF
    Loatingview () {if (Mfloatviewservice!= null) {mfloatviewservice.showfloat (); }/** * Hide suspension icon */public void Hidefloatingview () {if (Mfloatviewservice!= null) {Mfloatviewser
    Vice.hidefloat (); }/** * Release QDSDK data */public void Destroy () {if (Mfloatviewservice!= null) {mFloatViewService.h
      Idefloat ();
    Mfloatviewservice.destroyfloat ();
    } if (Mserviceconnectionmanager!= null) {Mserviceconnectionmanager.unbindfromservice (); } Mfloatviewservice = null; @Override public void onserviceconnected (componentname name, IBinder service) {Mfloatviewservice = (floatmenu
    Service.floatmenuservicebinder) service). GetService ();
    if (Mfloatviewservice!= null) {mfloatviewservice.showfloat ();
  @Override public void onservicedisconnected (componentname name) {mfloatviewservice = null;

 }
}

Floatmenuservice.java Code:

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], Andro Id.
    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) {Menuite
      Mview 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 Here simulate networking operations Mfloatmenu.startloadera
 Nim ();         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 () {//Use scene in service Packagemanager pm = This.getpackagemanager ();
    ApplicationInfo appInfo = FloatMenuService.this.getApplicationInfo (); drawable drawable = Appinfo.loadicon (PM);//Current app's logo String name = Appinfo.loadlabel (PM). toString ();/The name of the current app in Tent intent = pm.getlaunchintentforpackage (appinfo.packagename);//Current app's entry program 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 createdorupdat E, Object tag) {Toast.maketext (Floatmenuservice.this, Createdorupdate, Toast.length_shorT). Show (); @Override public void Createerror (String errormsg, Object tag) {Toast.maketext (floatme
          Nuservice.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 floatme
    Nuservice.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 Code:

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; Private final class<?
  Extends service> Service;
  Private Boolean attemptingtobind = false;
  Private Boolean bound = false;

  Private Qdserviceconnection mqdserviceconnection; Public Serviceconnectionmanager, Class&lt 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.onservicediscon
    Nected (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= "start floating menu via service"
    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= "Hide status bar and navigation bar"/>
</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"
      Prevent the oncreate--> <activity android:name= from being rerun when the screen is switched
    . 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 (library item) Structure:

Project Run as shown:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.