Android 簡單特殊知識點
View變灰色, 即實現灰度視圖
// 獲取當前activity 的根View
getWindow().getDecorView().findViewById(android.R.id.content)
// true 爲灰, false 爲正常
public void setGreyscale(View v, boolean greyscale) {
if (greyscale) {
// Create a paint object with 0 saturation (black and white)
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
Paint greyscalePaint = new Paint();
greyscalePaint.setColorFilter(new ColorMatrixColorFilter(cm));
// Create a hardware layer with the greyscale paint
v.setLayerType(View.LAYER_TYPE_HARDWARE, greyscalePaint);
} else {
// Remove the hardware layer
v.setLayerType(View.LAYER_TYPE_NONE, null);
}
}
手機震動
// 權限
<uses-permission android:name="android.permission.VIBRATE"/>
mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
mVibrator.vibrator(50); //震動50毫秒
mVibrator.cancle(); //停止震動
Webview 與 Js 交互
原理: html中寫明 需要調用 android 中的某個事件 即可。
http://blog.csdn.net/cappuccinolau/article/details/8262821
android 獲取設備型號、OS版本號:
import android.os.Build;
// .....
Build bd = new Build();
String model = bd.MODEL;
android.os.Build.MODEL
android.os.Build.VERSION.RELEASE
android 獲取設備Id等信息:
// 需要在AndroidManifest.xml中添加權限。
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
/*
* 電話狀態:
* 1.tm.CALL_STATE_IDLE=0 無活動
* 2.tm.CALL_STATE_RINGING=1 響鈴
* 3.tm.CALL_STATE_OFFHOOK=2 摘機
*/
tm.getCallState();//int
/*
* 電話方位:
*
*/
tm.getCellLocation();//CellLocation
/*
* 唯一的設備ID:
* GSM手機的 IMEI 和 CDMA手機的 MEID.
* Return null if device ID is not available.
*/
tm.getDeviceId();//String
/*
* 設備的軟件版本號:
* 例如:the IMEI/SV(software version) for GSM phones.
* Return null if the software version is not available.
*/
tm.getDeviceSoftwareVersion();//String
/*
* 手機號:
* GSM手機的 MSISDN.
* Return null if it is unavailable.
*/
tm.getLine1Number();//String
/*
* 附近的電話的信息:
* 類型:List
* 需要權限:android.Manifest.permission#ACCESS_COARSE_UPDATES
*/
tm.getNeighboringCellInfo();//List
/*
* 獲取ISO標準的國家碼,即國際長途區號。
* 注意:僅當用戶已在網絡註冊後有效。
* 在CDMA網絡中結果也許不可靠。
*/
tm.getNetworkCountryIso();//String
/*
* MCC+MNC(mobile country code + mobile network code)
* 注意:僅當用戶已在網絡註冊時有效。
* 在CDMA網絡中結果也許不可靠。
*/
tm.getNetworkOperator();//String
/*
* 按照字母次序的current registered operator(當前已註冊的用戶)的名字
* 注意:僅當用戶已在網絡註冊時有效。
* 在CDMA網絡中結果也許不可靠。
*/
tm.getNetworkOperatorName();//String
/*
* 當前使用的網絡類型:
* 例如: NETWORK_TYPE_UNKNOWN 網絡類型未知 0
NETWORK_TYPE_GPRS GPRS網絡 1
NETWORK_TYPE_EDGE EDGE網絡 2
NETWORK_TYPE_UMTS UMTS網絡 3
NETWORK_TYPE_HSDPA HSDPA網絡 8
NETWORK_TYPE_HSUPA HSUPA網絡 9
NETWORK_TYPE_HSPA HSPA網絡 10
NETWORK_TYPE_CDMA CDMA網絡,IS95A 或 IS95B. 4
NETWORK_TYPE_EVDO_0 EVDO網絡, revision 0. 5
NETWORK_TYPE_EVDO_A EVDO網絡, revision A. 6
NETWORK_TYPE_1xRTT 1xRTT網絡 7
*/
tm.getNetworkType();//int
/*
* 手機類型:
* 例如: PHONE_TYPE_NONE 無信號
PHONE_TYPE_GSM GSM信號
PHONE_TYPE_CDMA CDMA信號
*/
tm.getPhoneType();//int
/*
* Returns the ISO country code equivalent for the SIM provider's country code.
* 獲取ISO國家碼,相當於提供SIM卡的國家碼。
*
*/
tm.getSimCountryIso();//String
/*
* Returns the MCC+MNC (mobile country code + mobile network code) of the provider of the SIM. 5 or 6 decimal digits.
* 獲取SIM卡提供的移動國家碼和移動網絡碼.5或6位的十進制數字.
* SIM卡的狀態必須是 SIM_STATE_READY(使用getSimState()判斷).
*/
tm.getSimOperator();//String
/*
* 服務商名稱:
* 例如:中國移動、聯通
* SIM卡的狀態必須是 SIM_STATE_READY(使用getSimState()判斷).
*/
tm.getSimOperatorName();//String
/*
* SIM卡的序列號:
* 需要權限:READ_PHONE_STATE
*/
tm.getSimSerialNumber();//String
/*
* SIM的狀態信息:
* SIM_STATE_UNKNOWN 未知狀態 0
SIM_STATE_ABSENT 沒插卡 1
SIM_STATE_PIN_REQUIRED 鎖定狀態,需要用戶的PIN碼解鎖 2
SIM_STATE_PUK_REQUIRED 鎖定狀態,需要用戶的PUK碼解鎖 3
SIM_STATE_NETWORK_LOCKED 鎖定狀態,需要網絡的PIN碼解鎖 4
SIM_STATE_READY 就緒狀態 5
*/
tm.getSimState();//int
/*
* 唯一的用戶ID:
* 例如:IMSI(國際移動用戶識別碼) for a GSM phone.
* 需要權限:READ_PHONE_STATE
*/
tm.getSubscriberId();//String
/*
* 取得和語音郵件相關的標籤,即爲識別符
* 需要權限:READ_PHONE_STATE
*/
tm.getVoiceMailAlphaTag();//String
/*
* 獲取語音郵件號碼:
* 需要權限:READ_PHONE_STATE
*/
tm.getVoiceMailNumber();//String
/*
* ICC卡是否存在
*/
tm.hasIccCard();//boolean
/*
* 是否漫遊:
* (在GSM用途下)
*/
tm.isNetworkRoaming();//
獲取AndroidManifest.xml的信息
versionCode
// 可以用Context.gerPackageName()取得packageName
getPackageManager().getPackageInfo(packageName, 0).versionCode
versionName
getPackageManager().getPackageInfo(packageName, 0).versionName
getPackageManager().getPackageInfo(pName, PackageManager.GET_CONFIGURATIONS);
Android判斷應用是否存在
通過包名判斷
public boolean checkBrowser(String packageName) {
if (packageName == null || "".equals(packageName))
return false;
try {
ApplicationInfo info = getPackageManager().getApplicationInfo(
packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
通過Activity判斷
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.android.settings", //$NON-NLS-1$
"com.android.settings.InstalledAppDetails"); //$NON-NLS-1$
intent.putExtra("com.android.settings.ApplicationPkgName", //$NON-NLS-1$
mCurrentPkgName);
List acts = getPackageManager().queryIntentActivities(
intent, 0);
if (acts.size() > 0) {
startActivity(intent);
} else {
Toast.makeText(this,
getString(R.string.failed_to_resolve_activity),
Toast.LENGTH_SHORT).show();
}
獲取設備屏幕分辨率
首先我們需要用到的是DisplayMetrics這個類,它可以爲我們獲得手機屏幕屬性,這裏將其所在類導入。
import android.util.DisplayMetrics;
得到實例對象。
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
得到手機屏幕高度:
dm.heightPixels;
得到手機屏幕寬度:
dm.widthPixels;
得到以上手機屏幕的高度跟寬度後,即可以通過這兩個值按照比例還設定程序佈局中空間的大小。
獲取CPU序列號
/**
* 獲取CPU序列號
*
* @return CPU序列號(16位)
* 讀取失敗爲"0000000000000000"
*/
public static String getCPUSerial() {
String str = "", strCPU = "", cpuAddress = "0000000000000000";
try {
//讀取CPU信息
Process pp = Runtime.getRuntime().exec("cat /proc/cpuinfo");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
//查找CPU序列號
for (int i = 1; i
str = input.readLine();
if (str != null) {
//查找到序列號所在行
if (str.indexOf("Serial") > -1) {
//提取序列號
strCPU = str.substring(str.indexOf(":") + 1,
str.length());
//去空格
cpuAddress = strCPU.trim();
break;
}
}else{
//文件結尾
break;
}
}
} catch (IOException ex) {
//賦予默認值
ex.printStackTrace();
}
return cpuAddress;
}
獲取位置信息: locationManager
獲取LocationManager對象
String serviceString = Context.LOCATION_SERVICE;
LocationManager LocationManager =(LocationManager)getSystemService(serviceString);
選擇定位方法
- GPS_PROVIDER: GPS
- NETWORK_PROVIDER: NETWORK
// 以network爲例
String provider = LocationManager.NETWORK_PROVIDER
Location location = locationManager.getLaskKnownLocation(provider);
double lat = location.getLatitude();
double lng = location.getLongitude();
當前時間和時區
System.currentTimeMillis() //獲取當前時間
TimeZone.getDefault();// 時區