android 常用小功能(第二版)

1.获取当前的手机号

2.检查是否有网络连接

3.get请求网络数据

4.获取ctwap还是ctnet?

5.获取手机的IMSI码?

6.判断当前网络连接状态 ?

7.将字符串转化为ascii码

 

8.调用系统浏览器 9.判断手机当前运营商

10.判断应用在手机上的安装情况

11.将long型转化为byte数组

 

 

 

 

 

 

一、获取当前的手机号    

 

复制代码
/*
         * 获取当前的手机号
         */
        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;
    }
复制代码

你可能感兴趣的:(android 常用小功能(第二版))