android与电话相关代码片段

//直接调用短信接口发短信 
public void sendSMS(String phoneNumber,String message){  
        //获取短信管理器   
        android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();  
        //拆分短信内容(手机短信长度限制)    
        List<String> divideContents = smsManager.divideMessage(message);   
        for (String text : divideContents) {    
            smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI);    
        }  
    } 
 // 调起系统发短信功能 
 public void doSendSMSTo(String phoneNumber,String message){  
     if(PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){  
         Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));            
         intent.putExtra("sms_body", message);            
         startActivity(intent);  
     }  
 }    
//拨打电话
public static voiid dail(Context context,Strinig number){
	Intent intent = new Intent(Intent.ACTION_CALL);
	Uri data = Uri.parse("tel:"+ number);
	intent.setData(data);
	context.startActivity(intent);
}
//获取MCC+MNC代码 (SIM卡运营商国家代码和运营商网络代码)
//仅当用户已在网络注册时有效, CDMA 可能会无效(中国移动:46000 //46002, 中国联通:46001,中国电信:46003)
public static String getNetworkOperator(Context context) {
     TelephonyManager telephonyManager = (TelephonyManager) context
         .getSystemService(Context.TELEPHONY_SERVICE);
     return telephonyManager.getNetworkOperator();
   }
//返回移动网络运营商的名字
//(例:中国联通、中国移动、中国电信) 仅当用户已在网络注册时有效, //CDMA 可能会无效)
public static String getNetworkOperatorName(Context context) {
     TelephonyManager telephonyManager = (TelephonyManager) context
         .getSystemService(Context.TELEPHONY_SERVICE);
     return telephonyManager.getNetworkOperatorName();
}
//返回移动终端类型
PHONE_TYPE_NONE :0 手机制式未知
PHONE_TYPE_GSM :1 手机制式为GSM,移动和联通
PHONE_TYPE_CDMA :2 手机制式为CDMA,电信
PHONE_TYPE_SIP:3
public static int getPhoneType(Context context) {
     TelephonyManager telephonyManager = (TelephonyManager) context
         .getSystemService(Context.TELEPHONY_SERVICE);
     return telephonyManager.getPhoneType();
   }
//判断手机连接的网络类型(2G,3G,4G)
//联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDO
public class Constants {
   /**
    * Unknown network class
    */
   public static final int NETWORK_CLASS_UNKNOWN = 0 ;
   /**
    * wifi net work
    */
   public static final int NETWORK_WIFI = 1 ;
   /**
    * "2G" networks
    */
   public static final int NETWORK_CLASS_2_G = 2 ;
   /**
    * "3G" networks
    */
   public static final int NETWORK_CLASS_3_G = 3 ;
   /**
    * "4G" networks
    */
   public static final int NETWORK_CLASS_4_G = 4 ;
}

public static int getNetWorkClass(Context context) {
     TelephonyManager telephonyManager = (TelephonyManager) context
         .getSystemService(Context.TELEPHONY_SERVICE);
     switch (telephonyManager.getNetworkType()) {
     case TelephonyManager.NETWORK_TYPE_GPRS:
     case TelephonyManager.NETWORK_TYPE_EDGE:
     case TelephonyManager.NETWORK_TYPE_CDMA:
     case TelephonyManager.NETWORK_TYPE_1xRTT:
     case TelephonyManager.NETWORK_TYPE_IDEN:
       return Constants.NETWORK_CLASS_2_G;
     case TelephonyManager.NETWORK_TYPE_UMTS:
     case TelephonyManager.NETWORK_TYPE_EVDO_0:
     case TelephonyManager.NETWORK_TYPE_EVDO_A:
     case TelephonyManager.NETWORK_TYPE_HSDPA:
     case TelephonyManager.NETWORK_TYPE_HSUPA:
     case TelephonyManager.NETWORK_TYPE_HSPA:
     case TelephonyManager.NETWORK_TYPE_EVDO_B:
     case TelephonyManager.NETWORK_TYPE_EHRPD:
     case TelephonyManager.NETWORK_TYPE_HSPAP:
       return Constants.NETWORK_CLASS_3_G;
     case TelephonyManager.NETWORK_TYPE_LTE:
       return Constants.NETWORK_CLASS_4_G;
     default :
       return Constants.NETWORK_CLASS_UNKNOWN;
     }
   }
//判断当前手机的网络类型(WIFI还是2,3,4G)
//需要用到上面的方法
public static int getNetWorkStatus(Context context) {
     int netWorkType = Constants.NETWORK_CLASS_UNKNOWN;
     ConnectivityManager connectivityManager = (ConnectivityManager) context
         .getSystemService(Context.CONNECTIVITY_SERVICE);
     NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
     if (networkInfo != null && networkInfo.isConnected()) {
       int type = networkInfo.getType();
       if (type == ConnectivityManager.TYPE_WIFI) {
         netWorkType = Constants.NETWORK_WIFI;
       } else if (type == ConnectivityManager.TYPE_MOBILE) {
         netWorkType = getNetWorkClass(context);
       }
     }
     return netWorkType;
   }

猜你喜欢

转载自blog.csdn.net/zhuxingchong/article/details/79460232