Android開發之常用代碼片段


1、設置窗口格式為半透明
getWindow().setFormat(PixelFormat.TRANSLUCENT);
2、Android中在非UI線程里更新View的不同方法:
* Activity.runOnUiThread( Runnable )
* View.post( Runnable )
* View.postDelayed( Runnable, long )
* Hanlder
3、全屏顯示窗口
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
4、取得屏幕大小
方法A:
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
hAndW[0] = display.getWidth();
hAndW[1] = display.getHeight();
方法B:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
hAndW[0] = dm.widthPixels;
hAndW[1] = dm.heightPixels;
5、調瀏覽器 載入網址
Uri uri = Uri.parse("http://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
6、取得內存大小
ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(outInfo);
//可用內存
outInfo.availMem
//是否在低內存狀態
outInfo.lowMemory
取得ScrollView的實際高度
scrollview.getHeight()
scrollview.getMeasuredHeight()
scrollview.compute()
scrollview.getLayoutParams().height
7、監聽App安裝/卸載事件
A.Define a class derived from class BroadcastReceiver;
B.Register broadcast receiver;
MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_INSTALL);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
...
filter.addDataScheme("package"); //This line is very important. Otherwise, broadcast can't be received.
registerReceiver(myReceiver, filter);
Notes: The package name is Intent.mData. Intent.mData is not available in SDK 1.0, but it can be retrieved by calling Intent.getDataString();
8、取得IP地址
A.
//Connect via WIFI 通過wifi
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
B.
//Connect via GPRS通過gprs
public String getLocalIpAddress(){
try{
for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()){
return inetAddress.getHostAddress().toString();
}
}
}
}catch (SocketException ex){
Log.e(S.TAG, ex.toString());
}
return null;
}
9、ListView 后面adapter數據已更改,但是ListView沒有收到Notification
首先,必須將 更新adapter數據的代碼放在:Handler.post(Runnable)方法中執行;
然后,如果Adapter數據的來源如果是cursor(CursorAdapter)的話 可以cursor.requery一下,如果是別的可以強制調用一下notifyChange, notifyChange 會調用 invalidate 進行重繪;
10、模擬HOME鍵
Intent i=new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
11、設置焦點
editText.setFocusable(true);
editText.requestFocus();
editText.setFocusableInTouchMode(true);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM