Android开发常用小功能
Android开发常用小功能
发布时间:2016-03-01 来源:查字典编辑
摘要:一、获取当前的手机号/**获取当前的手机号*/publicStringgetLocalNumber(){TelephonyManagertM...

一、获取当前的手机号

/** 获取当前的手机号*/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()) {// 能连接Internetreturn 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 apps = pm.getInstalledPackages(PackageManager.GET_SIGNATURES);Iterator 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 yejiurui* @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.-----460007423945575System.out.println("-----"+telManager.getSimSerialNumber()); //1.-----89860089281174245575System.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>= 8;}return bytes;}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新安卓软件开发学习
热门安卓软件开发学习
编程开发子分类