編輯:關於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;
}
Android 大雜燴工程之ListView的開發2以及數據倉庫開發模式 今天是放完假後工作的第一天,為了證明我不是一段幽靈代碼,我給各位看客老爺們說句:祝大家節日過得
Android的Kotlin秘方(I):OnGlobalLayoutListener,春節後,又重新“開張”。各位高手請繼續支持。謝謝! &nbs
使用新版Android Studio檢測內存洩露和性能 內存洩露,是Android開發者最頭疼的事。可能一處小小的內存洩露,都可能是毀於千裡之堤的蟻穴。 怎麼才能檢測內
linux編程之socketTCP/IP協議及socket封裝socket編程的基本流程socket連接的建立(3次握手)socket連接的斷開(3次握手)由於TCP連接