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);
12、去掉平板导航
getWindow().getDecorView().setSystemUiVisibility(8);//只有某个特别机子有效
13、PowerManager
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "myLock");
wakeLock.acquire();
wakeLock.release();
下面定义的flag是在newWakeLock方法中要接收的参数,通过该flag,你可以定义系统的电源的展示效果。比如:
* cpu screen keyboard
* PARTIAL_WAKE_LOCK on off off
* SCREEN_DIM_WAKE_LOCK on dim off
* SCREEN_BRIGHT_WAKE_LOCK on bright off
* FULL_WAKE_LOCK on bright bright
这些flag是相互排斥的,一次只能定义一个。
如果你持有PARTIAL_WAKE_LOCK锁,不论任何定时器甚至是按下电源按钮,cpu都将继续运行,无法进入休眠状态。除非你释放掉它。
其他锁的话,虽然cpu也在运行,但是当用户按下电源按钮时,设备将立刻进入休眠状态。
正常情况下wakelocks实际上是没有被打开的,当需要时,它将通过特定的flag启动屏幕和键盘。 比如在应用中,涉及到向用户发送消息时,需要让用户立刻看到。此时会点亮屏幕。当WakeLock锁被释放的时候,activity的定时器将被重设,这将导致屏幕亮更长的时间。
14、SharedPreferences
SharedPreferences sp = getSharedPreferences(Constant.SERVUCE_APP, MODE_PRIVATE);
sp.putString(key,values);
Editor edit = sp.edit();
edit.commit();//修改后要记得提交
15、SQLite
实现这个抽象类SQLiteOpenHelper
DBHelper extends SQLiteOpenHelper
实现三个方法
DBHelper(Context context, String name, CursorFactory factory,int version, DatabaseErrorHandler errorHandler)
context //上下文 name//数据库名称 version //数据库的版本号
onCreate(SQLiteDatabase db)
第一次创建DBHelper执行onCreate方法
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
SQLite版本变化时执行onUpgrade再执行onCreate