Android开发小技巧集合(不断更新中)

先写下几个知道的,
[b]1.[/b]定时清理类似C:\Documents and Settings\Android123\.android下的.android文件夹
[b]2.[/b]可以通过settings----Language&software key进行是否在输入时显示软键盘的设置
[b]3.[/b]当进行代码输出调试的时候,有下面几种输出方式:
/* 打印出不同的log信息 */
Log.v(TAG, "VERBOSE");
Log.d(TAG, "DEBUG");
Log.i(TAG, "INFO");
Log.w(TAG, "WARN");
Log.e(TAG, "ERROR");

[b]4.[/b]Drawable文件夹相关
资源文件名字命名需要全部为小写形式,并且不能是单独的数字命名
在之前的版本中,只有一个drawable,而2.1版本中有drawable-mdpi、drawable-ldpi、drawable-hdpi三个,这三个主要是为了支持多分辨率。
drawable- hdpi、drawable- mdpi、drawable-ldpi的区别:
(1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854)
(2)drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x480)
(3)drawable-ldpi里面存放低分辨率的图片,如QVGA (240x320)
系统会根据机器的分辨率来分别到这几个文件夹里面去找对应的图片。在开发程序时为了兼容不同平台不同屏幕,建议各自文件夹根据需求均存放不同版本图片
[b]5.Context字面意思上下文[/b]:
位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄,很多方法需要通过Context才能识别调用者的实例,比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的实例为Activity,而到了一个button的onClick(View view)等方法时,我们用this时就会报错,所以我们可能使用ActivityName.this来解决,主要原因是因为实现Context的类主要有Android特有的几个模型,Activity、Service以及BroadcastReceiver。
常规需要Context实例的方法主要有各种Service实现的类,比如说SensorManager在实例化时需要getSystemService(String)方法就必须由Context的实例执行,还有一些私有的文件系统I/O比如说openFileInput以及常用的Toast的makeText方法等

6. [color=red][b]emulator: ERROR: the user data image is used by another emulator. aborting[/b][/color]
出现这种错误我在网上查了下,好像是由于程序没有正常关系导致的,至于更深层次的原因,我也还没搞清楚,如果哪位知道帮告知下
暂时处理方法:

adb kill-server
adb devices


7.设置窗口格式为半透明
getWindow().setFormat(PixelFormat.TRANSLUCENT);


8.取得屏幕大小

方法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;


9.取得内存大小
ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(outInfo);
//可用内存
outInfo.availMem
//是否在低内存状态
outInfo.lowMemory



10.监听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);


11.取得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;
}


12.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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值