1.每个Activity都有一个自己的window:
在activity的onCreate方法中,调用setContentView方法,其调用的是getWindow().setContentView()方法。
而getWindow()返回的Window 对象其实是Window抽象类的子类PhoneWindow.
mWindow = PolicyManager.makeNewWindow(this);
该句code是在Activity的attach()方法中调用。
2.在UI线程中调用invalidate()方法,即执行刷新操作—调用view 的onDraw()方法,但是如果在UI的子线程中,需要使用postInvalidate()方法。
3.应用程序写日志的Tag获取,如下:
private static String TAG = CameraTestActivity.class.getSimpleName();
4.java方法中可变参数支持,如下例子:
private static String findSettableValue(Collection<String> supportedValues,
String... desiredValues) {
String result = null;
if (supportedValues != null) {
for (String desiredValue : desiredValues) {
if (supportedValues.contains(desiredValue)) {
result = desiredValue;
break;
}
}
}
return result;
}
//调用:
1)
flashMode = findSettableValue(parameters.getSupportedFlashModes(),
Camera.Parameters.FLASH_MODE_TORCH, // string 常量"torch"
Camera.Parameters.FLASH_MODE_ON); // string常量"on"
2)
flashMode = findSettableValue(parameters.getSupportedFlashModes(),
Camera.Parameters.FLASH_MODE_OFF); // string常量"off"
5.分享接口:
Intent i=new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT,"这里是标题");
i.putExtra(Intent.EXTRA_TEXT, "这里是分享内容");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(i, "分享"));
6.使用应用程序包名启动外部应用:
//1)使用Intent的setComponent方法
Intent intent = new Intent();
intent.setComponent(new ComponentName(“包名”, “包名.主类名”));
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
//2)使用包管理器
Intent intent = new Intent();
intent = getPackageManager().getLaunchIntentForPackage(“包名”);
startActivity(intent);
7.使用Collections接口进行排序:
//1)定义一个排序的类:
private static class ByFirstStringComparator implements Comparator<String[]>, Serializable {
@Override
public int compare(String[] o1, String[] o2) {
return o1[0].compareTo(o2[0]);
}
}
//2)使用:
Collections.sort(listArray, new ByFirstStringComparator());
8.再按一次后退键退出应用程序:
private static Boolean isExit = false;
private static Boolean hasTask = false;
Timer tExit = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
isExit = false;
hasTask = true;
}
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("TabHost_Index.java onKeyDown");
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(isExit == false ) {
isExit = true;
Toast.makeText(this, "再按一次后退键退出应用程序", Toast.LENGTH_SHORT).show();
if(!hasTask) {
tExit.schedule(task, 2000);
}
} else {
finish();
System.exit(0);
}
}
return false;
}
9.开启和关闭Android APN网络,即接入点设置
http://www.cnblogs.com/luxiaofeng54/archive/2011/05/03/2035392.html
10.eclipse xml 编辑设置:
1)修改Eclipse的XML格式化配置
这一步的配置是使格式化的效果为控件的每个属性配置占一行。进入 Window/Preferences,展开到 XML/XML Files/Editor,
勾选 Split multiple attributes each on a new line
经此配置后,每次使用快捷键 Ctrl+Shift+F 键格式化后每个属性配置就会占一行。
2)压缩节点的声明方式
这步的目的是将没有子节点的元素的声明方式进行压缩,如将 <TextView ...></TextView>转化为 <TextView .../>。
方法为在XML文件内空白地方点击鼠标右键,选择 Source/Cleanup Document...
11.杀死后台进程
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("name.of.your.package");
需要一个权限
12.获取手机型号和系统版本号
<span style="margin: 0px; padding: 0px; border: 0px; color: rgb(204, 204, 204); background: transparent;"></span><pre name="code" class="java" style="margin-top: 0px; margin-bottom: 10px;">String sdk=android.os.Build.VERSION.SDK;// SDK号
String model=android.os.Build.MODEL; // 手机型号
String release=android.os.Build.VERSION.RELEASE;
// android系统版本号
<pre name="code" class="java" style="margin-top: 0px; margin-bottom: 10px;">// 获取手机的DeviceID,即手机的唯一标识。
// GSM网络:IMEI号
// ESN,CDMA网络:MEID号
// 获取不到时,返回null
// 需要权限:READ_PHONE_STATE
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
13.使用Handler发送消息:
1) 创建Handler:
Handler mHandler = new Handler(){
public void handleMessage(Message msg){
swtich(msg.what){
case msgId:
// 消息为msgId的处理
break;
case msgId2:
// 消息为msgId2的处理
break;
default:
break;
}
}
}
2) 程序中发送消息:
Message m = new Message();
m.what=msgid;
handler.sendMessage(m);
如果需要传递数据,可以使用如下:
Bundle b = new Bundle();
b.putString(key,value);
b.putString(key1,value2);
m.setData(b);
handler.sendMessage(b);
14.使用SharePreferences 保存数据:
1) 从文件中获取值:
// 得到SharePreferences对象,每个相同的sharedFileName返回的是相同的对象,即单例模式。
//0:Context.MODE_PRIVATE
//1:Context.MODE_WORLD_READABLE
//2:Context.MODE_WORLD_WRITEABLE
//3:Context.MODE_APPEND
//4:Context.MODE_MULTI_PROCESS
//8:Context.MODE_ENABLE_WRITE_AHEAD_LOGGING
SharedPreferences configSharedPref = mContext.getSharedPreferences("sharedFileName",0);
String str = configSharedPref.getString("key","defaultValue");
2) 修改保存的数据:
SharePreferences.Editor configEditor = configSharedPref.edit();
configEditor.putString("key","new value");
configEdit.commit();
15.创建桌面快捷方式:
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));
shortcutIntent.putExtra("duplicate", false);
Intent appIntent = new Intent(mContext, MainActivity.class);
appIntent.setAction("android.intent.action.MAIN");
appIntent.addCategory("android.intent.category.LAUNCHER");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, appIntent);
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,iconRes);
sendBroadcast(shortcutIntent);
16.Android接入点相关内容:
1.检测wifi时候可用:
boolean enabled = false;
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null) {
enabled = wifiManager.isWifiEnabled();
}
if(wifiManager.getConnectionInfo().getIpAddress() == 0) {
enabled = false;
}
}
2.获取代理网关信息:
String proxyHost = android.net.Proxy.getDefaultHost();
int port = android.net.Proxy.getDefaultPort();
注意:
代理网关的获取,只有接入点设置成wap时,才可以获取到。比如移动接入点设置成cmwap,获取到的信息是:
ip地址:10.0.0.172,端口号:80
net和wifi接入点:获取到的ip为null,端口号:-1