"Android" Project common feature collection (i)

Source: Internet
Author: User
Tags rand string format
in the future will be more commonly used in the collection of small functions, improve the efficiency of development later, good memory star than rotten pen, good writing blog, good study. 1. Verify EditText
/**
     * < determine if edittext is null >
     * @param edtext *
     @return *
     @see [Class, Class # method, Class # member]/public
    static Boolean isemptyedittext (EditText edtext)
    {
        if (Edtext.gettext (). toString (). Trim (). Length () > 0)
            return false;
        else return
            true;
    }

/**
     * < Verify mailbox is valid >
     * @param email
     * @return
     * @see [Class, Class # method, Class # member]/public
    static Boolean isemailidvalid (string email)
    {
        string expression = ' ^[\\w\\.-]+@ ([\\w\\-]+\\.) +[a-z]{2,4}$ ";
        Charsequence inputstr = email;

        Pattern pattern = pattern.compile (expression, pattern.case_insensitive);
        Matcher Matcher = Pattern.matcher (INPUTSTR);

        if (Matcher.matches ()) return
            true;
        else return
            false;
    

2. Check Network connection Status

/**
     * Check network connection Status
     *
     * @param context
     * @return true or false */public
    Static Boolean Isnetworkavailable (Context context)
    {

        Connectivitymanager cm = (Connectivitymanager) Context.getsystemservice (context.connectivity_service);
        Networkinfo netInfo = Cm.getactivenetworkinfo ();
        if (netInfo!= null && netinfo.isconnectedorconnecting ())
        {return
            true;
        }
        return false;
    }
3. Get the application icon
/**
     * < Get the currently applied icon >
     * @param mcontext
     * @return
     * @see [Class, Class # method, Class # member]/public
    static Drawable Getappicon (context Mcontext)
    {
        drawable icon = null;
        Final Packagemanager PM = Mcontext.getpackagemanager ();
        String PackageName = Mcontext.getpackagename ();
        Try
        {
            icon = Pm.getapplicationicon (PackageName);
            return icon;
        }
        catch (namenotfoundexception E1)
        {
            e1.printstacktrace ();
        }
        return null;
    }
4. Send local alerts
/** * Send local notification * * @param mcontext context * @param notice of the title * @param Notice of the content * @param click on the notification to open intent */@SuppressLint ("Newapi") @SuppressWarnings ({"Static-access"}) public static void Sendlocatnotification (C
        Ontext Mcontext, string title, String message, Intent mintent) {int appiconresid = 0;
        Pendingintent pintent = null; if (mintent!= null) pintent = pendingintent.getactivity (mcontext, 0, Mintent, Pendingintent.flag_update_curre

        NT);
        Final Packagemanager PM = Mcontext.getpackagemanager ();
        String PackageName = Mcontext.getpackagename ();
        ApplicationInfo ApplicationInfo;
            try {applicationinfo = Pm.getapplicationinfo (PackageName, packagemanager.get_meta_data);
        Appiconresid = Applicationinfo.icon;
        catch (Namenotfoundexception E1) {e1.printstacktrace (); }//Notification Notification = new notification. Builder (Mcontext)//. Setsmallicon (Appiconresid). Setwhen (System.currenttimemillis ())//. Setcontenttitle (t

        itle). Setcontenttext (message)//. Setcontentintent (Pintent). GetNotification ();
        Notification Notification; if (mintent = = null) {notification = new Notification.builder (mcontext). Setsmallicon (
                    APPICONRESID). Setwhen (System.currenttimemillis ()). Setcontenttitle (Message)
                    . SetStyle (New Notification.bigtextstyle (). BigText (Message)). Setautocancel (True) . Setcontenttext (Message). Setcontentintent (pendingintent.getactivity (mcontext, 0, New in

        Tent (), 0)). GetNotification (); else {notification = new Notification.builder (mcontext). Setsmallicon (Appico
     NRESID). Setwhen (System.currenttimemillis ())               . Setcontenttitle (Message). Setcontenttext (Message). Setautocancel (t Rue). SetStyle (New Notification.bigtextstyle (). BigText (Message)). setcontentintent
        (pintent). GetNotification ();
        //Click to clear the notification notification.flags |= notification.flag_auto_cancel;
        Turn on the notification prompt tone notification.defaults |= notification.default_sound;

        Open vibration Notification.defaults |= notification.default_vibrate;
        Notificationmanager manager = (Notificationmanager) mcontext.getsystemservice (Mcontext.notification_service);
        Manager.notify (0, notification);
    Manager.notify (r.string.app_name, notification); }

5. Random Access

    /**
     * < random access letter a-z>
     * @return Random num
     * @see [Class, Class # method, Class # member]/public
    Static Char Getrandomcharacter ()
    {
        Random r = new Random ();
        char C = (char) (R.nextint (num) + ' a ');

        return c;
    }

    /**
     * < get random numbers between 0 and number >
     * @param no.
     * @return
     * @see [Class, Class # method, Class # member]
    * * public static int Getrandom (int number)
    {
        Random rand = new Random ();
        return Rand.nextint (number);
    }
6. Open Date and time picker
private static Calendar DateTime = Calendar.getinstance (); /** * Open Date selector * * @param mcontext * @param format * @param mtextview to be displayed TextView * * Pub
        Lic static void Showdatepickerdialog (final context Mcontext, final String format, final TextView Mtextview) { New Datepickerdialog (Mcontext, New Ondatesetlistener () {@Override public void Ondateset (  DatePicker view, int year, int monthofyear, int dayofmonth) {SimpleDateFormat dateformatter =
                New SimpleDateFormat (format);

                Datetime.set (year, monthofyear, DayOfMonth);
            Mtextview.settext (Dateformatter.format (Datetime.gettime ()). ToString ());
    }, Datetime.get (Calendar.year), Datetime.get (Calendar.month), Datetime.get (Calendar.day_of_month)). Show ();
     /** * Time Selector * * @param mcontext * @param mtextview to display TextView * @return Show Timepicker * * public static void Showtimepickerdialog (final context Mcontext, final TextView mtextview) {new Timepickerdialo G (Mcontext, New Ontimesetlistener () {@Override public void Ontimeset (Timepicker view, in
                T hourofday, int minute) {SimpleDateFormat timeformatter = new SimpleDateFormat ("hh:mm a");
                Datetime.set (Calendar.hour_of_day, hourofday);

                Datetime.set (Calendar.minute, MINUTE);
            Mtextview.settext (Timeformatter.format (Datetime.gettime ()). ToString ());
    }, Datetime.get (Calendar.hour_of_day), Datetime.get (Calendar.minute), false. Show (); }

7. Get the equipment wide and high

/**
     * get device height
     *
     * @param mcontext
     * @return Equipment Height
    /public static int Getdeviceheight Mcontext)
    {
        Displaymetrics displaymetrics = new Displaymetrics ();
        (activity) mcontext). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (displaymetrics);
        return displaymetrics.heightpixels;
    }

    /**
     * Get device width
     *
     * @param mcontext
     * @return Device width
    /public static int Getdevicewidth ( Context Mcontext)
    {
        Displaymetrics displaymetrics = new Displaymetrics ();
        (activity) mcontext). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (displaymetrics);
        return displaymetrics.widthpixels;
    }

8.String and list turn each other

/** * <string converted to list> * @param string * @return * @see [Class, Class # method, Class # member] */public static list<string> Stringtoarraylist (String string) {list<string> strvaluelist
        = new Arraylist<string> ();
        Strvaluelist = Arrays.aslist (String.Split (","));
    return strvaluelist; /** * <list converted to string> * @param string * @return * @see [Class, Class # method, Class # member]/public s
        Tatic string arraylisttostring (list<string> List) {string strvalue = null;
        StringBuilder sb = new StringBuilder ();
            for (String s:list) {sb.append (S + ",");
        strvalue = Sb.tostring (); } if (Strvalue.length () > 0 && strvalue.charat (strvalue.length ()-1) = = ', ') {St
        RValue = strvalue.substring (0, Strvalue.length ()-1);
    return strvalue; }
9.Bitmap and drawable Mutual turn
/**
     * 
     <drawable bitmap>
     * @param mcontext
     * @param drawable *
     @return
     * @see [Class, Class # method, Class #成员]
     *
    /public static Bitmap Drawabletobitmap (context mcontext, int drawable)
    {
        drawable mydrawable = Mcontext.getresources (). getdrawable (drawable);
        Return ((bitmapdrawable) mydrawable). Getbitmap ();
    }

    /**
     * 
     <bitmap drawable>
     * @param mcontext
     * @param drawable *
     @return
     * @see [Class, Class # method, Class # member]
     *
    /public static drawable bitmaptodrawable (context Mcontext, Bitmap Bitmap)
    {
        return New Bitmapdrawable (bitmap);
    }
10. Get the application version number
/** * < Get Application Version number > * @param mcontext * @return * @see [Class, Class # method, Class # member] *
        /public static int Getappversioncode (context mcontext) {PackageInfo pinfo = null;
        try {pinfo = Mcontext.getpackagemanager (). Getpackageinfo (Mcontext.getpackagename (), 0);
        catch (Namenotfoundexception e) {e.printstacktrace ();
    return pinfo.versioncode; }
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.