Android Development Common small features

Source: Internet
Author: User

First, get the current mobile phone number

* * Get current cell phone number/public String Getlocalnumber () {Telephonymanager Tmanager = (Telephonymanager)   . Getsystemservice (Telephony_service);   String number = Tmanager.getline1number ();   ; return number; }


Second, check if there is a network connection


public boolean checkinternet () {Connectivitymanager cm = (Connectivitymanager) this. Getsystemservice (conte Xt.   Connectivity_service);   Networkinfo info = Cm.getactivenetworkinfo ();   if (info!= null && info.isconnected ()) {//can connect to Internet return true;   else {//cannot connect to return false; }   }


third, GET request network data


public static string GetDate (string url) {HttpGet get = new HttpGet (URL);   HttpClient client = new Defaulthttpclient ();   try {httpresponse response = Client.execute (get);//Return Entityutils.tostring (Response.getentity ());   catch (Exception e) {return null; }   }


Obtain the signature information of the program


//This is the signature information for obtaining the APK package private String getsign (context context) {Packagemanager pm = Context.getpackagemanager ();   List<packageinfo> apps = Pm.getinstalledpackages (Packagemanager.get_signatures);   Iterator<packageinfo> iter = Apps.iterator ();   while (Iter.hasnext ()) {PackageInfo PackageInfo = Iter.next ();   String PackageName = packageinfo.packagename;   return packageinfo.signatures[0].tocharsstring (); if (Packagename.equals (Instance.getpackagename ())) {Mediaapplication.logd (Downloadapk.class,   Packageinfo.signatures[0].tocharsstring ());   return packageinfo.signatures[0].tocharsstring ();   } return null; }

The signature information returned by the
results in a long string of data


four, get Ctwap or ctnet?


/** * Get the current access point is Ctwap or ctnet * @author <a href= "mailto:yejiurui@126.com" >yejiurui</a> * @versio   N 1.0 2013-5-17 pm 5:46:05 2013/Private String ctwap= "Ctwap";   Private String ctnet= "ctnet";   Private URI Preferred_apn_uri = URI. Parse ("CONTENT://TELEPHONY/CARRIERS/PREFERAPN");   public string Getapntype {string apntype = "NoMatch";   Cursor C = context.getcontentresolver (). query (Preferred_apn_uri, NULL, NULL, NULL, NULL);   C.movetofirst ();   String user = c.getstring (c.getcolumnindex ("APN"));   if (User.startswith (ctnet)) {apntype = ctnet;   else if (User.startswith (Ctwap)) {apntype = Ctwap;   return apntype; }


Five, get the phone IMSI code?


Get the phone's IMSI code Telephonymanager telmanager= (Telephonymanager) Getsystemservice (Telephony_service); SYSTEM.OUT.PRINTLN ("-----" +telmanager.getsubscriberid ()); 2.-----460007423945575 System.out.println ("-----" +telmanager.getsimserialnumber ());   1.-----89860089281174245575 System.out.println ("-----" +telmanager.getsimoperator ());   SYSTEM.OUT.PRINTLN ("-----" +telmanager.getsimcountryiso ());   SYSTEM.OUT.PRINTLN ("-----" +telmanager.getsimoperatorname ()); SYSTEM.OUT.PRINTLN ("-----" +telmanager.getsimstate ());


Six, determine the current network connection status?


/** * To determine the current network connection status * @param context * @return/public static Boolean isnetworkconnected (context Contex T) {Networkinfo networkinfo = (connectivitymanager) context. Getapplicationcontext (). Getsystemservice ("Connectivity   ")). Getactivenetworkinfo ();   if (networkinfo!= null) {return networkinfo.isconnectedorconnecting ();   return false; }


Seven, convert a string to an ASCII code?


package com.my.page;   public class Stringtoascii {private static String tohexutil (int n) {string rt= "";   Switch (n) {case 10:rt+= "A";   Case 11:rt+= "B";   Case 12:rt+= "C";   Case 13:rt+= "D";   Case 14:rt+= "E";   Case 15:rt+= "F";   Default:rt+=n;   return RT;   public static String Tohex (int n) {StringBuilder sb=new StringBuilder ();   if (n/16==0) {return tohexutil (n);   }else{String T=tohex (N/16);   int nn=n%16;   Sb.append (t). Append (Tohexutil (NN));   return sb.tostring ();   public static string Parseascii (String str) {StringBuilder sb=new StringBuilder ();   Byte[] Bs=str.getbytes ();   for (int i=0;i<bs.length;i++) sb.append (Tohex (bs[i));   return sb.tostring ();   public static void Main (string args[]) {string s= "xyz";   System.out.println ("Converted string is:" +stringtoascii.parseascii (s)); }   }


Eight, call the system browser


Call the System browser Intent intent= new Intent ();   Intent.setaction ("Android.intent.action.VIEW");   Uri Content_url = Uri.parse (Exiturl);   Intent.setdata (Content_url); StartActivity (Intent);


Nine, Judge mobile phone current operator


Get the phone's IMSI code telephonymanager Telmanager = (telephonymanager) getsystemservice (Telephony_service);   String Sendnum=null;   String Imsi=telmanager.getsubscriberid ();; if (IMSI!= null) {if (Imsi.startswith ("46000") | | imsi.startswith ("46002")) {//Because IMSI has run out of mobile network number 46000, virtual a 46002   The number 134/159th segment uses this number Toast.maketext (Getapplicationcontext (), "The current mobile number is sent", 0). Show ();   SENDNUM=CONSTANTS.SENDNUMBER_GD;   else if (Imsi.startswith ("46001")) {//China Unicom Toast.maketext (Getapplicationcontext (), "Current Unicom number Sent", 0). Show ();   SENDNUM=CONSTANTS.SENDNUMBER_GD;   else if (Imsi.startswith ("46003")) {//China Telecom Toast.maketext (Getapplicationcontext (), "Current telecommunications number sent out", 0). Show ();   SENDNUM=CONSTANTS.SENDNUMBER_JT; }}else{sendnum=constants.sendnumber_jt;//Group number}


10. Judge the installation of the application on the phone


/** * To determine the application in the mobile phone installation situation * @param packagename to determine the application of the package name */Private Boolean checkapk (String packagename) {LIST<PA   ckageinfo> Pakageinfos = Getpackagemanager (). Getinstalledpackages (Packagemanager.get_uninstalled_packages);   for (PackageInfo Pi:pakageinfos) {String pi_packagename = pi.packagename;   if (Packagename.endswith (Pi_packagename)) {return true;   return false; }


11. Converts a long type to a byte array.   * @return returns the byte array to convert input parameters to/public static byte[] Long2bytes (long L) {byte[] bytes = new Byte[8];   int i = 8;   for (byte b:bytes) {b = (byte) (L & 0xff);   Bytes[--i] = b;   L >>= 8;   return bytes; }
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.