Common small functions in Android Development

Source: Internet
Author: User

Common small functions in Android Development

1. Obtain the current mobile phone number

 

/*

* Get the current mobile phone number

*/

Public String getLocalNumber (){

TelephonyManager tManager = (TelephonyManager) this

. GetSystemService (TELEPHONY_SERVICE );

String number = tManager. getLine1Number ();

; Return number;

}
 

2. check whether there is a network connection
 

Public boolean checkInternet (){

ConnectivityManager cm = (ConnectivityManager) this

. GetSystemService (Context. CONNECTIVITY_SERVICE );

NetworkInfo info = cm. getActiveNetworkInfo ();

If (info! = Null & info. isConnected ()){

// Connect to the Internet

Return true;

} Else {

// Cannot be connected

Return false;

}

}
 

3. get request network data
 

Public static String GetDate (String url ){

HttpGet = new HttpGet (url );

HttpClient client = new DefaultHttpClient ();

Try {

HttpResponse response = client.exe cute (get );//

Return EntityUtils. toString (response. getEntity ());

} Catch (Exception e ){

Return null;

}

}
 

Obtain the signature information of a program.
 

// Obtain the signature information of 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 returned signature is a long string of data.
 

4. Get ctwap or ctnet?
 

/**

* Obtain whether the current access point is ctwap or ctnet.

* @ Author <a href = "mailto: yejiurui@126.com"> yejiurui </a>

* @ Version 1.0 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 (Context context ){

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;

}
 

5. Get the IMSI code of the mobile phone?
 

// Obtain the IMSI code of the mobile phone

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 ());
 

6. determine the current network connection status?
 

/**

* Determine the current network connection status

* @ Param context

* @ Return

*/

Public static boolean isNetworkConnected (Context context ){

NetworkInfo networkInfo = (ConnectivityManager) context

. GetApplicationContext (). getSystemService ("connectivity "))

. GetActiveNetworkInfo ();

If (networkInfo! = Null ){

Return networkInfo. isConnectedOrConnecting ();

}

Return false;

}
 

7. 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"; break;

Case 11: rt + = "B"; break;

Case 12: rt + = "C"; break;

Case 13: rt + = "D"; break;

Case 14: rt + = "E"; break;

Case 15: rt + = "F"; break;

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 ("the converted string is:" + StringToAscii. parseAscii (s ));

}

}
 

8. 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 );
 

9. determine the current mobile phone operator
 

// Obtain the IMSI code of the mobile phone

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 under mobile network number 46000 is used up, A 46002 number is virtualized, and this number is used in segment 134/159.

Toast. makeText (getApplicationContext (), "the current mobile number has been sent", 0). show ();

SendNum = Constants. sendNumber_GD;

} Else if (imsi. startsWith ("46001") {// China Unicom

Toast. makeText (getApplicationContext (), "the current Unicom number has been sent", 0). show ();

SendNum = Constants. sendNumber_GD;

} Else if (imsi. startsWith ("46003") {// China Telecom

Toast. makeText (getApplicationContext (), "the current telecom number has been sent", 0). show ();

SendNum = Constants. sendNumber_JT;

}

} Else {

SendNum = Constants. sendNumber_JT; // group number

}
 

10. determine the application installation status on the mobile phone
 

/**

* Determine the installation status of the application on your mobile phone.

* @ Param packageName determine the application package name

*/

Private boolean checkAPK (String packageName ){

List <PackageInfo> 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. Convert the long type into a byte array.

* @ Return returns the byte array converted from the input parameter

*/

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.