android常用代码片段
2010-09-01 11:00
363 查看
1、设置窗口格式为半透明
getWindow().setFormat(PixelFormat.TRANSLUCENT);
2、Android中在非UI线程里更新View的不同方法:
* Activity.runOnUiThread( Runnable )
* View.post( Runnable )
* View.postDelayed( Runnable, long )
* Hanlder
3、取得屏幕大小
方法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;
4、取得内存大小
ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(outInfo);
//可用内存
outInfo.availMem
//是否在低内存状态
outInfo.lowMemory
取得ScrollView的实际高度
scrollview.getHeight()
scrollview.getMeasuredHeight()
scrollview.compute()
scrollview.getLayoutParams().height
5、监听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();
6、取得IP地址
A.Connect via WIFI
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
B.Connect via 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;
}
7、Monitor File System Event: Open/Create..
android.os.FileObserver
8、Monitor Media Event: mount/unmount..
A.Define a class derived from class BroadcastReceiver;
B.Register broadcast receiver;
MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
...
filter.addDataScheme("file"); //This is important. Otherwise, broadcast can't be received
registerReceiver(myReceiver, filter);
About other broadcast actions for media, and extra data bind to the
intent, please refer to http://developer.android.com/reference/android/content/Intent.html
(please search ACTION_MEDIA_ on this web page)
Notes: The path of mounted media is Intent.mData. Intent.mData is not
available in SDK 1.0, but it be retrieved by calling
Intent.getDataString();
9、ListView 后面adapter数据已更改,但是ListView没有收到Notification
首先,必须将 更新adapter数据的代码放在:Handler.post(Runnable)方法中执行;
然后,如果Adapter数据的来源如果是cursor(CursorAdapter)的话 可以cursor.requery一下,如果是别的可以强制调用一下notifyChange, notifyChange 会调用 invalidate 进行重绘;
getWindow().setFormat(PixelFormat.TRANSLUCENT);
2、Android中在非UI线程里更新View的不同方法:
* Activity.runOnUiThread( Runnable )
* View.post( Runnable )
* View.postDelayed( Runnable, long )
* Hanlder
3、取得屏幕大小
方法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;
4、取得内存大小
ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(outInfo);
//可用内存
outInfo.availMem
//是否在低内存状态
outInfo.lowMemory
取得ScrollView的实际高度
scrollview.getHeight()
scrollview.getMeasuredHeight()
scrollview.compute()
scrollview.getLayoutParams().height
5、监听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();
6、取得IP地址
A.Connect via WIFI
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
B.Connect via 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;
}
7、Monitor File System Event: Open/Create..
android.os.FileObserver
8、Monitor Media Event: mount/unmount..
A.Define a class derived from class BroadcastReceiver;
B.Register broadcast receiver;
MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
...
filter.addDataScheme("file"); //This is important. Otherwise, broadcast can't be received
registerReceiver(myReceiver, filter);
About other broadcast actions for media, and extra data bind to the
intent, please refer to http://developer.android.com/reference/android/content/Intent.html
(please search ACTION_MEDIA_ on this web page)
Notes: The path of mounted media is Intent.mData. Intent.mData is not
available in SDK 1.0, but it be retrieved by calling
Intent.getDataString();
9、ListView 后面adapter数据已更改,但是ListView没有收到Notification
首先,必须将 更新adapter数据的代码放在:Handler.post(Runnable)方法中执行;
然后,如果Adapter数据的来源如果是cursor(CursorAdapter)的话 可以cursor.requery一下,如果是别的可以强制调用一下notifyChange, notifyChange 会调用 invalidate 进行重绘;
相关文章推荐
- Android:常用代码片段整理
- Android开发之常用代码片段
- 【常用JAVA代码片段】在android中如何打开文件节点,写节点
- 转--Android实用的代码片段 常用代码总结
- Android开发之常用代码片段
- Android 开发常用代码片段
- Android常用代码片段(笔记一)
- Android 开发常用代码片段
- 36个Android开发常用代码片段
- Android---常用代码片段的分享
- android 常用UI使用工具代码片段
- Android 中常用代码片段
- 36个Android开发常用代码片段
- Android 开发常用代码片段
- Android常用代码片段(一)
- Android开发之常用代码片段
- Android 开发常用代码片段
- Android 常用代码片段
- 36个Android开发常用代码片段