Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android開發常用小功能

Android開發常用小功能

編輯:關於android開發

Android開發常用小功能


 一、獲取當前的手機號

 

  /*

  * 獲取當前的手機號

  */

  public String getLocalNumber() {

  TelephonyManager tManager = (TelephonyManager) this

  .getSystemService(TELEPHONY_SERVICE);

  String number = tManager.getLine1Number();

  ;                return number;

  }
 

  二、檢查是否有網絡連接
 

  public boolean checkInternet() {

  ConnectivityManager cm = (ConnectivityManager) this

  .getSystemService(Context.CONNECTIVITY_SERVICE);

  NetworkInfo info = cm.getActiveNetworkInfo();

  if (info != null && info.isConnected()) {

  // 能連接Internet

  return true;

  } else {

  // 不能連接到

  return false;

  }

  }
 

  三、get請求網絡數據
 

  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;

  }

  }
 

  獲取程序的簽名信息
 

  //這是獲取apk包的簽名信息

  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;

  }
 

  返回的簽名信息結果為很長的一串數據
 

  四、獲取ctwap還是ctnet?
 

  /**

  * 獲取當前的接入點是ctwap還是ctnet

  * @author <a href="mailto:[email protected]">yejiurui</a>

  * @version 1.0 2013-5-17 下午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;

  }
 

  五、獲取手機的IMSI碼?
 

  //獲取手機的IMSI碼

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

  六、判斷當前網絡連接狀態?
 

  /**

  * 判斷當前網絡連接狀態

  * @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;

  }
 

  七、將字符串轉化為Ascii碼?
 

  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("轉換後的字符串是:"+StringToAscii.parseAscii(s));

  }

  }
 

  八、調用系統浏覽器
 

  //調用系統浏覽器

  Intent intent= new Intent();

  intent.setAction("android.intent.action.VIEW");

  Uri content_url = Uri.parse(exitUrl);

  intent.setData(content_url);

  startActivity(intent);
 

  九、判斷手機當前運營商
 

  // 獲取手機的IMSI碼

  TelephonyManager telManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

  String sendNum=null;

  String imsi=telManager.getSubscriberId();;

  if (imsi != null) {

  if (imsi.startsWith("46000")|| imsi.startsWith("46002")) {

  // 因為移動網絡編號46000下的IMSI已經用完,所以虛擬了一個46002編號,134/159號段使用了此編號

  Toast.makeText(getApplicationContext(), "當前移動號碼發送完畢", 0).show();

  sendNum=Constants.sendNumber_GD;

  } else if (imsi.startsWith("46001")) {// 中國聯通

  Toast.makeText(getApplicationContext(), "當前聯通號碼發送完畢", 0).show();

  sendNum=Constants.sendNumber_GD;

  } else if (imsi.startsWith("46003")) {// 中國電信

  Toast.makeText(getApplicationContext(), "當前電信號碼發送完畢", 0).show();

  sendNum=Constants.sendNumber_JT;

  }

  }else{

  sendNum=Constants.sendNumber_JT;//集團號碼

  }
 

  十.判斷應用在手機上的安裝情況
 

  /**

  * 判斷該應用在手機中的安裝情況

  * @param packageName  要判斷應用的包名

  */

  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;

  }
 

  十一.將long型轉化為byte數組.

  * @return  返回將輸入參數轉化為的字節數組

  */

  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;

  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved