Common to method summary in Android development

Source: Internet
Author: User

1. Determine if the service is running
 public static Boolean isservicerunning (context context, String ServiceName) {
   boolean isrunning = false;
  activitymanager 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 if the memory card is plugged in
public static Boolean isexternalstorageavailable () {
String state = Environment.getexternalstoragestate ();
if (Environment.MEDIA_MOUNTED.equals (state)) {
return true;
} else {
return false;
}
}

3. Get the path to 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. Get the phone IMEI
 public static String Getimei (Context ctx) {
  if (ctx!=null) {
    string IMEI = "";
   telephonymanager Telephonymanager = (telephonymanager) CTX
      . Getsystemservice (Context.telephony_service);
   if (Telephonymanager! = null)
    imei = Telephonymanager.getdeviceid ( );
   if (IMEI = = "")
    imei = "0";
   return IMEI;
  }else{
   return null;
  }
 }

5. Get the phone number
public static String Getphonenumber (Context ctx) {
if (ctx!=null) {
String number = "";
Telephonymanager Telephonymanager = (telephonymanager) ctx
. Getsystemservice (Context.telephony_service);
if (Telephonymanager! = null)
Number = Telephonymanager.getline1number ();
if (number = = "")
Number = "0";
return number;
}else{
return null;
}
}

6. Get 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. Get Software version information
public static String getsoftwareversion (Context ctx) {
String Version = "";
try {
PackageInfo 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 date = new Date ();
SimpleDateFormat from = new SimpleDateFormat (Str_format);
String times = From.format (date);
return times;
}

9. Get Bitmap by 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 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. Check all non-system applications in your phone

public static list<packageinfo> Getallapps (context context) {

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

Packagemanager Pmanager = Context.getpackagemanager ();

Get all apps in your phone

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

for (int i = 0; i < paklist.size (); i++) {

PackageInfo Pak = (packageinfo) paklist.get (i);

Determine if a non-system preinstalled application

if ((Pak.applicationInfo.flags & Pak.applicationInfo.FLAG_SYSTEM) <= 0) {

Customs applications

Apps.add (Pak);

}

}

return apps;

}

13. Find all apps that support sharing in your phone


public static list<resolveinfo> Getshareapps (context 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. Get the MAC address and IP address of the Andorid mobile 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"

;

}

Get all non-system apps on your phone

public static arraylist<appinfo> Getinstalledpackages (context context)

{

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

list<packageinfo> packages = Context.getpackagemanager ()

. getinstalledpackages (0);

for (int i = 0; i < packages.size (); i++)

{

PackageInfo 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 non-system application, add to Applist

Applist.add (Tmpinfo);

}

}

return applist;

}

16. Stream into a byte array

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;

}

Travel Network www.jieberu.com

Collect free tickets on the push-push family www.tuituizu.com

Common to method summary in Android development

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.