Summary of commonly used methods in Android Development

Source: Internet
Author: User

1. Determine whether the service is running
 Public static Boolean isservicerunning (context, string servicename ){
  Boolean isrunning = false;
  Activitymanager = (activitymanager) Context
    . Getsystemservice (context. activity_service );
  List <activitymanager. runningserviceinfo> servicelist = activitymanager
    . Getrunningservices (integer. max_value );
  If (! (Servicelist. Size ()> 0 )){
   Return false;
  }
  For (INT I = 0; I <servicelist. Size (); I ++ ){
   Runningserviceinfo Si = (runningserviceinfo) servicelist. Get (I );
   If (Si. Service. getclassname (). Equals (servicename )){
    Isrunning = true;
    Break;
   }
  }
  Return isrunning;
 }

2. Determine whether the memory card is inserted
 Public static Boolean isexternalstorageavailabLe (){
  String state = environment. getexternalstoragestate ();
  If (environment. media_mounted.equals (State )){
   Return true;
  } Else {
   Return false;
  }
 }

3. Obtain the path of the file
 Public static string getsdcardpath (string Foldername ){
  If (Foldername! = NULL ){
   String Path = environment. getexternalstoragedirectOry ()
   . Getabsolutepath () + file. Separator + Foldername;
   File file = new file (PATH );
   If (! File. exists ()){
    File. mkdir ();
   }
   Return path;
  } Else {
   Return NULL;
  }
 }

4. Obtain the IMEI of the mobile phone
 Public static string getimei (context CTX ){
  If (CTX! = NULL ){
   String IMEI = "";
   Telephonymanager = (telephonymanager) CTX
     . Getsystemservice (context. telephony_service );
   If (telephonymanager! = NULL)
    IMEI = telephonymanager. getdeviceid ();
   If (IMEI = "")
    IMEI = "0 ";
   Return IMEI;
  } Else {
   Return NULL;
  }
 }

5. Obtain the mobile phone number
 Public static string getphonenumber (context CTX ){
  If (CTX! = NULL ){
   String number = "";
   Telephonymanager = (telephonymanager) CTX
   . Getsystemservice (context. telephony_service );
   If (telephonymanager! = NULL)
    Number = telephonymanager. getline1number ();
   If (number = "")
    Number = "0 ";
   Return number;
  } Else {
   Return NULL;
  }
 }

6. Obtain the mobile phone status information.
 Public static string fetch_status (context CTX ){
  Telephonymanager TM = (telephonymanager) CTX
    . Getsystemservice (context. telephony_service );//

  String STR = "";
  STR + = "DeviceID (IMEI) =" + TM. getdeviceid () + "\ n ";
  STR + = "devicesoftwareversion =" + TM. getdevicesoftwareversion()
    + "\ N ";
  STR + = "line1number =" + TM. getline1number () + "\ n ";
  STR + = "networkcountryiso =" + TM. getnetworkcountryiso () + "\ n ";
  STR + = "networkoperator =" + TM. getnetworkoperator () + "\ n ";
  STR + = "networkoperatorname =" + TM. getnetworkoperatorname () + "\ n ";
  STR + = "networktype =" + TM. getnetworktype () + "\ n ";
  STR + = "honetype =" + TM. getphonetype () + "\ n ";
  STR + = "simcountryiso =" + TM. getsimcountryiso () + "\ n ";
  STR + = "simoperator =" + TM. getsimoperator () + "\ n ";
  STR + = "simoperatorname =" + TM. getsimoperatorname () + "\ n ";
  STR + = "simserialnumber =" + TM. getsimserialnumber () + "\ n ";
  STR + = "simstate =" + TM. getsimstate () + "\ n ";
  STR + = "subscriberid (imsi) =" + TM. getsubscriberid () + "\ n ";
  STR + = "voicemailnumber =" + TM. getvoicemailnumber () + "\ n ";
  Return STR;
 }

7. Obtain the software version information.
 Public static string getsoftwareversion (context CTX ){
  String version = "";
  Try {
   Packageinfo = CTX. getpackagemanager (). getpackageinfo (
     CTX. getpackagename (), 0 );
   Version = packageinfo. versionname;
  } Catch (packagemanager. namenotfoundexception e ){
   E. printstacktrace ();
  }
  Return version;
 }

8. Get the current date.
 Public static string getcurrdate (string format ){
  String str_format = (format = NULL | format = "")? "Yyyy-mm-dd hh: mm: SS"
    : Format;
  Date = new date ();
  Simpledateformat from = new simpledateformat (str_format );
  String times = from. Format (date );
  Return times;
 }

9. Get Bitmap Based on URL
 Public static bitmap geturlbitmap (string uripic) throws exception {
  URL imageurl = NULL;
  Bitmap bitmap = NULL;
  Try {
   Imageurl = new URL (uripic );
  } Catch (malformedurlexception e ){
   E. printstacktrace ();
  }
  Try {
   Httpurlconnection conn = (httpurlconnection) imageurl
     . Openconnection ();
   Conn. setconnecttimeout (5*1000 );
    Conn. setreadtimeout (5*1000 );
    Conn. setdoinput (true );
    Conn. setdooutput (true );
    Conn. setusecaches (true );
    Conn. setrequestmethod ("Post ");
   Conn. Connect ();
   Inputstream is = conn. getinputstream ();
   Bitmap = bitmapfactory. decodestream (is );
   Is. Close ();
  } Catch (ioexception e ){
   E. printstacktrace ();
  }
  Return bitmap;
 }

10. Hide the app
 Public static void hiddenapp (context CTX ){
  Packagemanager PM = CTX. getpackagemanager ();
  Resolveinfo homeinfo = PM. resolveactivity (
    New intent (intent. action_main)
      . Addcategory (intent. category_home), 0 );
  Activityinfo AI = homeinfo. activityinfo;
  Intent startintent = new intent (intent. action_main );
  Startintent. addcategory (intent. category_launcher );
  Startintent. setcomponent (New componentname (AI. packagename, AI. Name ));
  CTX. startactivity (startintent );
 }

11. Exit the program (3 methods)
 Public static void exitapp (context CTX ){

  Activitymanager AM = (activitymanager)
  CTX. getsystemservice (context. activity_service );
  Am. restartpackage (CTX. getpackagename ());
  
  Intent startmain = new intent (intent. action_main );
  Startmain. addcategory (intent. category_home );
  Startmain. setflags (intent. flag_activity_new_task );
  CTX. startactivity (startmain );
  Android. OS. process. killprocess (Android. OS. process. mypid ());
  System. Exit (0 );
  
  Android. OS. process. killprocess (Android. OS. process. mypid ());
 }

12. query all non-system applications on the mobile phone

 

 PublicStatic list <packageinfo> getallapps (context ){

 List <packageinfo> apps = new arraylist <packageinfo> ();

 Packagemanager pmanager = context. getpackagemanager ();

 // Obtain all applications on the mobile phone

 List <packageinfo> paklist = pmanager. getinstalledpackages (0 );

 For(INT I =
0; I <paklist. Size (); I ++ ){

 Packageinfo Pak = (packageinfo) paklist. Get (I );

 // Determine whether the application is pre-installed.

 If(Pak. applicationinfo. Flags & Pak. applicationinfo. flag_system) <=
0 ){

 // Customs applications

 Apps. Add (PAK );

 }

 }

 ReturnApps;

 }

 13. query all apps that support sharing on the mobile phone

 Public static list <resolveinfo> getmediaapps (context)
 {
  List <resolveinfo> mapps = new arraylist <resolveinfo> ();
  Intent intent = new intent (intent. action_send, null );
  Intent. addcategory (intent. category_default );
  Intent. settype ("text/plain ");
  Packagemanager pmanager = context. getpackagemanager ();
  Mapps = pmanager
    . Queryintentactivities (intent, packagemanager. component_enabled_state_default );
  Return mapps;
 }

 14. Obtain the MAC address and IP address of the andorid mobile phone WiFi connection

  Public static string getinfo ()

 {

 Wifimanager WiFi = (wifimanager) getsystemservice (context. wifi_service );

 Wifiinfo info = wifi. getconnectioninfo ();

 

 String maxtext = info. getmacaddress ();

 String iptext = inttoip (info. getipaddress ());

 String status = "";

 If (WiFi. getwifistate () = wifimanager. wifi_state_enabled)

 {

 Status = "wifi_state_enabled ";

 }

 String SSID = info. getssid ();

 Int networkid = info. getnetworkid ();

 Int speed = info. getlinkspeed ();

 Return "Mac:" + maxtext + "\ n \ r"

 + "IP:" + iptext + "\ n \ r"

 + "WiFi status:" + status + "\ n \ r"

 + "SSID:" + SSID + "\ n \ r"

 + "Net work ID:" + networkid + "\ n \ r"

 + "Connection speed:" + speed + "\ n \ r"

 ;

 }

15. Retrieve all non-system applications on the mobile phone

Public static arraylist <appinfo> getinstalledpackages (context)

{

Arraylist <appinfo> applist = new arraylist <appinfo> ();

List <packageinfo> packages = context. getpackagemanager ()

. Getinstalledpackages (0 );

For (INT I = 0; I <packages. Size (); I ++)

{

Packageinfo = packages. Get (I );

Appinfo tmpinfo = new appinfo ();

Tmpinfo. appname = packageinfo. applicationinfo. loadlabel (

Context. getpackagemanager (). tostring ();

Tmpinfo. packagename = packageinfo. packagename;

Tmpinfo. versionname = packageinfo. versionname;

Tmpinfo. versioncode = packageinfo. versioncode;

Tmpinfo. appicon = packageinfo. applicationinfo

. Loadicon (context. getpackagemanager ());

If (packageinfo. applicationinfo. Flags & applicationinfo. flag_system) = 0)

 {

// If the application is not a system application, add it to applist.

 Applist. Add (tmpinfo );

 }

}

Return applist;

}

16. convert data into byte Arrays

Public byte [] stream2bytes (inputstream is) throws ioexception {

Byte [] Mbyte;

Bytearrayoutputstream baos = new bytearrayoutputstream ();

Byte [] buffer = new byte [1024];

Int length = 0;

While (length = is. Read (buffer, 0, 1024 ))! =-1 ){

Baos. Write (buffer, 0, length );

}

Baos. Flush ();

Mbyte = baos. tobytearray ();

Baos. Close ();

Return Mbyte;

}

 

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.