Android中获取系统的一些信息以及一些小功能
来源:互联网 发布:.net软件开发程序员 编辑:程序博客网 时间:2024/06/10 05:09
一.获取手机内存的使用情况(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);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if(requestCode==0){Bitmap bitmap = data.getParcelableExtra("data");img.setImageBitmap(bitmap);}}
三. 2次back退出
private long currentTime;@Overridepublic 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");//获取ViewGroupViewGroup 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);//实例化一个ImageSpanImageSpan span = new ImageSpan(this, bitmap);SpannableString spannableString = new SpannableString("face");spannableString.setSpan(span, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//添加editText.append(spannableString);
- Android中获取系统的一些信息以及一些小功能
- Android小功能 -- 获取系统内存信息以及进程信息
- 获取android系统的一些功能
- Android中 获取手机信息以及联网状态的一些工具类小结
- Android开发中一些琐碎的小功能的实现
- 获取SQL Server的一些系统信息
- 获取系统的一些信息汇总
- 获取erlang系统信息的一些方法
- 获取手机一些系统信息的一些工具类
- Android系统中Debug的一些小技巧
- unity3D中一些小功能的实现
- Java获取一些系统信息
- android的一些常用手机信息获取
- android的一些常用手机信息获取
- android 获取手机的一些硬件信息
- android的一些常用手机信息获取
- 转: 获取Android手机的一些信息
- android 获取手机信息的一些知识
- 使用NDK为Android写linux bin执行程式二三事
- 现今最强引擎对比!虚幻3 vs CE2 vs 寒霜2.0
- UIColor,CGColor,CIColor三者的区别和联系
- C艹 仅以此日志帮助那些从AS3转到2dx不习惯的战友
- 达人浅析国内游戏引擎发展史
- Android中获取系统的一些信息以及一些小功能
- java多线程运用notify和wait(修改)
- eclipse.ini内存设置
- cassandra学习笔记4--Cassandra Java客户端
- Perl中的正则表达式
- __declspec(novtable)以及虚函数调用
- 认真对待 懂得放弃
- jQuery.extend 函数详解
- php html文档解析器simple_html_dom