android常用代码合集,Android开发常用经典代码段集锦
本文实例总结了Android开发常用经典代码段。分享给大家供大家参考,具体如下:
1、图片旋转
Bitmap bitmapOrg = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.moon);
Matrix matrix = new Matrix();
matrix.postRotate(-90);//旋转的角度
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
bitmapOrg.getWidth(), bitmapOrg.getHeight(), matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
2、获取手机号码
//创建电话管理
TelephonyManager tm = (TelephonyManager)
//与手机建立连接
activity.getSystemService(Context.TELEPHONY_SERVICE);
//获取手机号码
String phoneId = tm.getLine1Number();
//记得在manifest file中添加
android:name="android.permission.READ_PHONE_STATE" />
//程序在模拟器上无法实现,必须连接手机
3.格式化string.xml 中的字符串
// in strings.xml..
Thanks for visiting %s. You age is %d!
// and in the java code:
String.format(getString(R.string.my_text), "oschina", 33);
4、android设置全屏的方法
A.在java代码中设置
/** 全屏设置,隐藏窗口所有装饰 */
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
B、在AndroidManifest.xml中配置
android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
5、设置Activity为Dialog的形式
在AndroidManifest.xml中配置Activity节点是配置theme如下:
android:theme="@android:style/Theme.Dialog"
6、检查当前网络是否连上
ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
在AndroidManifest.xml 增加权限:
7、检测某个Intent是否有效
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
8、android 拨打电话
try {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:+110"));
startActivity(intent);
} catch (Exception e) {
Log.e("SampleApp", "Failed to invoke call", e);
}
9、android中发送Email
Intent i = new Intent(Intent.ACTION_SEND);
//i.setType("text/plain"); //模拟器请使用这行
i.setType("message/rfc822") ; // 真机上使用这行
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@gmail.com","test@163.com});
i.putExtra(Intent.EXTRA_SUBJECT,"subject goes here");
i.putExtra(Intent.EXTRA_TEXT,"body goes here");
startActivity(Intent.createChooser(i, "Select email application."));
10、android中打开浏览器
Intent viewIntent = new
Intent("android.intent.action.VIEW",Uri.parse("http://vaiyanzi.cnblogs.com"));
startActivity(viewIntent);
11、android 获取设备唯一标识码
String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
12、android中获取IP地址
public String getLocalIpAddress() {
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses();
enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
13、android获取存储卡路径以及使用情况
/** 获取存储卡路径 */
File sdcardDir=Environment.getExternalStorageDirectory();
/** StatFs 看文件系统空间使用情况 */
StatFs statFs=new StatFs(sdcardDir.getPath());
/** Block 的 size*/
Long blockSize=statFs.getBlockSize();
/** 总 Block 数量 */
Long totalBlocks=statFs.getBlockCount();
/** 已使用的 Block 数量 */
Long availableBlocks=statFs.getAvailableBlocks();
14 android中添加新的联系人
private Uri insertContact(Context context, String name, String phone) {
ContentValues values = new ContentValues();
values.put(People.NAME, name);
Uri uri = getContentResolver().insert(People.CONTENT_URI, values);
Uri numberUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
values.clear();
values.put(Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);
values.put(People.NUMBER, phone);
getContentResolver().insert(numberUri, values);
return uri;
}
15、查看电池使用情况
Intent intentBatteryUsage = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
startActivity(intentBatteryUsage);
16、获取进程号
ActivityManager mActivityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List mRunningProcess = mActivityManager.getRunningAppProcesses();
int i = 1;
for (ActivityManager.RunningAppProcessInfo amProcess : mRunningProcess) {
Log.e("homer Application", (i++) + " PID = " + amProcess.pid + "; processName = " + amProcess.processName);
}
希望本文所述对大家Android程序设计有所帮助。
android常用代码合集,Android开发常用经典代码段集锦相关推荐
- android常用代码合集,Android常用代码
1.图片旋转 Bitmap bitmapOrg = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable. ...
- Python表白代码合集:5种表白代码、比小心心、无限弹窗、520表白墙、抖音热门表白小软件、无套路表白
一.介绍一下 写了5个Python表白代码,代码很简单,通俗易懂,都有注释.有什么问题联系本人QQ:483062431.文章最后教你们怎么打包成exe,如果你懒得搞懂代码怎么回事 ...
- Python表白代码合集:5种表白代码,找不到对象你来找我,这也太秀了叭
文章目录 一.容我啰嗦两句 二.来吧,代码展示 1.给女神比个小心心 2.无限弹窗式表白 3.这货不是表白代码,悄悄送给你们 4.520表白墙 5.抖音热门表白小软件 6.无套路表白 三.写在最后 一 ...
- 史上最全的Android文章精选合集
用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你能获得这些料: 知道setContentView()之后发生了什么? ... Andr ...
- 掘金 Android 文章精选合集
掘金官方 2017.07.10 16:42* 字数 175276 阅读 46638 评论 12 喜欢 653 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有 ...
- 写给Android App开发人员看的Android底层知识合集(1-8)
写给Android App开发人员看的Android底层知识合集(1-8) 转自包老师:http://www.cnblogs.com/Jax/p/6864103.html 写给Android App开 ...
- 一、PyTorch Cookbook(常用代码合集)
PyTorch Cookbook(常用代码合集) 原文链接:https://mp.weixin.qq.com/s/7at6y2NcYaxGGN8syxlccA 谢谢作者的付出.
- GitHub上7000+ Star的Python常用代码合集
作者 | 二胖并不胖 来源 | 大数据前沿(ID:bigdataqianyan) 今天二胖给大家介绍一个由一个国外小哥用好几年时间维护的Python代码合集.简单来说就是,这个程序员小哥在几年前开始保 ...
- 云栖Android精华文章合集
云栖Android精彩文章整理自各位技术大咖们关于Android的精彩分享,本文将云栖Android精彩文章整理成为一个合集,以便于大家学习参考.Weex.apk瘦身.开发资源.应用维护.内存管理,一 ...
- 设计师常用软件合集,有一个你肯定知道!
大家好,我是极客猿小兵,今天给大家推荐设计师常用软件合集,时至今日,设计师可以细分为很行岗位,有平面设计,品牌设计,UI设计,交互设计,游戏UI设计,网页设计,电商设计,插画设计,视频制作等等. 这里 ...
最新文章
- 该怎样才能将PDF转换成HTML
- unity从入门到精通下载_左手Unity右手Unreal
- 消除类游戏ccf c语言,ccf试题 消除类游戏
- Cordova Embedded Server
- 元组的常用方法、应用场景
- 离线搭建安卓环境 adt
- Lstm(循环神经网络)
- vue项目中引入外部字体(超详细)
- 用python做外贸
- 1200PLC学习资料整理
- zstuACM 问题 H: 超级跳跳跳1281
- 计算机工程师标准着装,工程师穿什么样的衣服才好看
- 多用户商城系统有哪些盈利模式?
- [UE5蓝图基础一]13.类似”人类一败涂地”掉落一定距离会回到空中 最终着落点还是设定地形上
- Python 简单银行系统
- Ubuntu 使用 create_ap 开启 wifi 热点
- oracle consistent gets,consistentgets在Oracle使用特例
- css文件的创建和使用
- 大牛deep learning集合
- 替换word中手动换行(软回车)为段落标记(硬回车)
热门文章
- 大学生必备软件免费自学视频教程,让那些培训班都见鬼去吧
- 金蝶生成凭证模板_金蝶k3凭证生成
- linux系统能运行iis吗,Linux 下可以安装 IIS 吗
- C语言程序书写中 每行必须有分号,C语言课后习题答案
- VBXtraLib 1.0 下载
- java p2p实例_java文件p2p传输
- java小程序(万年历)
- 百度杀毒软件2013正式发布
- picGo图片上传到码云失败,报错404-{“message”:“Branch”}的解决方法
- 清理注册表 php,cad注册表怎么删干净