Android开发常用经典代码段集锦
程序员文章站
2024-02-23 13:53:58
本文实例总结了android开发常用经典代码段。分享给大家供大家参考,具体如下:
1、图片旋转
bitmap bitmaporg = bitmapfactory...
本文实例总结了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中添加 <uses-permission android:name="android.permission.read_phone_state" /> //程序在模拟器上无法实现,必须连接手机
3.格式化string.xml 中的字符串
// in strings.xml.. <string name="my_text">thanks for visiting %s. you age is %d!</string> // 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中配置
<activity android:name=".login.netedit" android:label="@string/label_net_edit" android:screenorientation="portrait" android:theme="@android:style/theme.black.notitlebar.fullscreen"> <intent-filter> <action android:name="android.intent.net_edit" /> <category android:name="android.intent.category.default" /> </intent-filter> </activity>
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 增加权限:
<uses-permission android:name="android.permission.access_network_state" />
7、检测某个intent是否有效
public static boolean isintentavailable(context context, string action) { final packagemanager packagemanager = context.getpackagemanager(); final intent intent = new intent(action); list<resolveinfo> 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<networkinterface> en = networkinterface.getnetworkinterfaces(); en.hasmoreelements();) { networkinterface intf = en.nextelement(); for (enumeration<inetaddress> 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<activitymanager.runningappprocessinfo> 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多媒体操作技巧汇总(音频,视频,录音等)》
希望本文所述对大家android程序设计有所帮助。