您的位置:首页 > 移动开发 > Android开发

Android中获取系统的一些信息以及一些小功能

2013-11-21 16:13 423 查看
一.获取手机内存的使用情况(VM heap)

// 程序总共的内存
int maxMemory = (int) Runtime.getRuntime().maxMemory();
// 已经从系统拿出来的内存,但是没用
int freeMemory = (int) Runtime.getRuntime().freeMemory();
// 已经使用的总内存
int totalMemory = (int) Runtime.getRuntime().totalMemory();


二.从相册中获取一张图片并且进行裁剪

public void click(View view){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT,null);
intent.setType("image/*");//获取任意的图片类型
intent.putExtra("crop", "true");//滑动选中图片区域
intent.putExtra("aspectX", 3);//表示剪切框的比例1:1的效果
intent.putExtra("aspectY",5);
intent.putExtra("outputX", 300);//制定输出图片的大小
intent.putExtra("outputY",500);
intent.putExtra("return-data",true);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0){
Bitmap bitmap = data.getParcelableExtra("data");
img.setImageBitmap(bitmap);
}
}

三. 2次back退出

private long currentTime;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (System.currentTimeMillis() - currentTime < 2000) {
finish();
}
currentTime = System.currentTimeMillis();
return true;
}
return false;
}

四、通过HttpPost请求网络数据

//第一步创建HttpPost对象
HttpPost post = new HttpPost("url");
// 设置连接超时
post.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5 * 1000);
// 获取数据超时
post.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,5 * 1000);
//设置post请求参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("",""));
//设置发送数据的编码格式
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//实例化一个默认的请求客户端
HttpClient client = new DefaultHttpClient();
//执行请求
HttpResponse response = client.execute(post);
//请求返回码
int statusCode = response.getStatusLine().getStatusCode();
//请求返回的结果
if (statusCode == 200) {
String result = EntityUtils.toString(response.getEntity(),"UTF-8");
}

五、通过HttpURLConnection请求网络数据

URL url = new URL("URL");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();//通过url打开连接
conn.setRequestMethod("POST");//设置请求方式
conn.setDoInput(true);// 允许下载数据
conn.setDoOutput(true);// 允许上传数据
conn.setRequestProperty("Connection", "Keep-Alive");//是否需要持久连接
conn.setRequestProperty("Charset", "UTF-8");//设置编码格式
conn.getOutputStream().write("".getBytes());//设置要传输的数据
if (conn.getResponseCode() == 200) {
//获取收到的数据
InputStream is = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
if ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
//得到的数据
byte[] result = baos.toByteArray();
is.close();
baos.close();
}

六、自定义文本的字体

//设置字体
public void setFont(ViewGroup group, Typeface font) {
int count = group.getChildCount();
View v;
for (int i = 0; i < count; i++) {
v = group.getChildAt(i);
if (v instanceof TextView || v instanceof EditText
|| v instanceof Button) {
((TextView) v).setTypeface(font);
} else if (v instanceof ViewGroup)
setFont((ViewGroup) v, font);
}
}

//从Asset文件夹中读取字体
Typeface mFont = Typeface.createFromAsset(getAssets(), "STCAIYUN.TTF");
//获取ViewGroup
ViewGroup root = (ViewGroup) findViewById(R.id.rl);
//给ViewGroup设置字体
setFont(root, mFont);

七、MarginLayoutParams

//获取View原始的参数
MarginLayoutParams mlp = (MarginLayoutParams) view.getLayoutParams();
//重新设置参数
view.setMargins(-mlp.leftMargin, mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);

八、截屏

public void click(View view){
//设置为1秒钟后执行截屏操作,避免截下按钮点击的动作
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
//获取窗口图像
View v = getWindow().getDecorView();
v.setDrawingCacheEnabled(true);
v.buildDrawingCache();
Bitmap bitmap = v.getDrawingCache();
//获取状态栏的高度
Rect fram = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(fram);
int statusBarHeight = fram.top;
//获取屏幕图像的高度
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
int width = outMetrics.widthPixels;
int height = outMetrics.heightPixels;
//创建新的图像(去掉状态栏的高度)
Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width, height-statusBarHeight);
//保存图片到SDcard下
try {
FileOutputStream fos = new FileOutputStream(File.createTempFile("capture", ".jpg", new File("mnt/sdcard")));
bitmap2.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}, 1000);
}


九、TextView中插入表情
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resouceId);
//实例化一个ImageSpan
ImageSpan span = new ImageSpan(this, bitmap);
SpannableString spannableString = new SpannableString("face");
spannableString.setSpan(span, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//添加
editText.append(spannableString);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: