网友通过本文主要向大家介绍了android知识点总结,android知识点,android重要知识点,android基础知识点,android开发知识点等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
android小知识点代码片段
1 拨打电话的操作 播打电话号码
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
2 发送短信的操作 短信过长时 拆分短信 一条短信最大的文本长度 是多少 ? 中文 70 汉字 英文 160字符
SmsManager smsmanager = SmsManager.getDefault();
/*
*sentIntent, deliveryIntent延期的意图 ,
*sentintent 发送报告
*deliveryIntent 送达报告
*/
ArrayList messages = smsmanager.divideMessage(content);
for(String message : messages){
smsmanager.sendTextMessage(number, null, message, null, null);
}
3.检测sd卡状态,并往sd卡中写数据。需要权限
//MEDIA_UNKNOWN:不能识别sd卡
//MEDIA_REMOVED:没有sd卡
//MEDIA_UNMOUNTED:sd卡存在但是没有挂载
//MEDIA_CHECKING:sd卡正在准备
//MEDIA_MOUNTED:sd卡已经挂载,可用
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
//返回一个File对象,其路径是sd卡的真实路径
File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write((name + "##" + pass).getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
else{
Toast.makeText(this, "sd卡不可用哟亲么么哒", 0).show();
}
}
4.判断sd卡剩余容量。
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize;
long totalBlocks;
long availableBlocks;
//获取当前系统版本的等级
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){ //4.3版本后开始起作用
blockSize = stat.getBlockSizeLong();
totalBlocks = stat.getBlockCountLong();
availableBlocks = stat.getAvailableBlocksLong();
} else{ //否则使用旧的api
blockSize = stat.getBlockSize();
totalBlocks = stat.getBlockCount();
availableBlocks = stat.getAvailableBlocks();
}
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(formatSize(availableBlocks * blockSize));
5使用xml序列化器生成xml文件
//1.拿到序列化器对象
XmlSerializer xs = Xml.newSerializer();
//2.初始化
File file = new File("sdcard/sms2.xml");
try {
FileOutputStream fos = new FileOutputStream(file);
//enconding:指定用什么编码生成xml文件
xs.setOutput(fos, "utf-8");
//3.开始生成xml文件
//enconding:指定头结点中的enconding属性的值
xs.startDocument("utf-8", true);
xs.startTag(null, "message");
for (Message sms : smsList) {
xs.startTag(null, "sms");
xs.startTag(null, "body");
xs.text(sms.getBody() + "
"); xs.endTag(null, "body"); xs.startTag(null, "date"); xs.text(sms.getDate()); xs.endTag(null, "date"); xs.startTag(null, "type"); xs.text(sms.getType()); xs.endTag(null, "type"); xs.startTag(null, "address"); xs.text(sms.getAddress()); xs.endTag(null, "address"); xs.endTag(null, "sms"); } xs.endTag(null, "message"); //告诉序列化器,文件生成完毕 xs.endDocument(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
6.解析xml文件
//获取到src文件夹下的资源文件
InputStream is = getClassLoader().getResourceAsStream("weather.xml");
//拿到pull解析器对象
XmlPullParser xp = Xml.newPullParser();
//初始化
try {
xp.setInput(is, "gbk");
//获取当前节点的事件类型,通过事件类型的判断,我们可以知道当前节点是什么节点,从而确定我们应该做什么操作
int type = xp.getEventType();
City city = null;
while(type != XmlPullParser.END_DOCUMENT){
//根据节点的类型,要做不同的操作
switch (type) {
case XmlPullParser.START_TAG:
// 获取当前节点的名字
if("weather".equals(xp.getName())){
//创建city集合对象,用于存放city的javabean
cityList = new ArrayList();
}
else if("city".equals(xp.getName())){
//创建city的javabean对象
city = new City();
}
else if("name".equals(xp.getName())){
// 获取当前节点的下一个节点的文本
String name = xp.nextText();
city.setName(name);
}
else if("temp".equals(xp.getName())){
// 获取当前节点的下一个节点的文本
}
else if("pm".equals(xp.getName())){
// 获取当前节点的下一个节点的文本
}
break;
case XmlPullParser.END_TAG:
if("city".equals(xp.getName())){
}
break;
}
//把指针移动到下一个节点,并返回该节点的事件类型
type = xp.next();
}
} catch (Exception e) {
e.printStackTrace();
}
7 listview优化
1)复用convertView
View v = null;
//判断条目是否有缓存
if(convertView == null){
//把布局文件填充成一个View对象
v = View.inflate(MainActivity.this, R.layout.item_listview, null);
}else{
v = convertView;
}
2)利用viewHolder,返回一个View对象,作为listview的条目显示至界面
public View getView(int position, View convertView, ViewGroup parent) {
News news = newsList.get(position);
View v = null;
ViewHolder mHolder;
if(convertView == null){
v = View.inflate(MainActivity.this, R.layout.item_listview, null);
mHolder = new ViewHolder();
//把布局文件中所有组件的对象封装至ViewHolder对象中
mHolder.tv_title = (TextView) v.findViewById(R.id.tv_title);
mHolder.tv_detail = (TextView) v.findViewById(R.id.tv_detail);
mHolder.tv_comment = (TextView) v.findViewById(R.id.tv_comment);
mHolder.siv = (SmartImageView) v.findViewById(R.id.iv);
//把ViewHolder对象封装至View对象中
v.setTag(mHolder);
}else{
v = convertView;
mHolder = (ViewHolder) v.getTag();
}
//给三个文本框设置内容
mHolder.tv_title.setText(news.getTitle());
mHolder.tv_detail.setText(news.getDetail());
mHolder.tv_comment.setText(news.getComment() + "条评论");
//给新闻图片imageview设置内容
mHolder.siv.setImageUrl(news.getImageUrl());
return v;
}
class ViewHolder{
//条目的布局文件中有什么组件,这里就定义什么属性
TextView tv_title;
TextView tv_detail;
TextView tv_comment;
SmartImageView siv;
}
8 junit 测试框架的使用
1)在manifest中添加上下列代码
2)在application下添加以下代码
3)创建测试类继承AndroidTestCase类
4)编写测试方法
10 采用get方式提交数据 原理:拼装url
String param1 = URLEncoder.encode(name);
String param2 = URLEncoder.encode(password);
URL url = new URL(path + "?name=" + param1 + "&password=" + param2);
HttpURLConnection conn = (HttpURLConnection) url.openConnecti