本文實(shí)例總結(jié)了android開發(fā)常用經(jīng)典代碼段。分享給大家供大家參考,具體如下:
1、圖片旋轉(zhuǎn)
1
2
3
4
5
6
|
bitmap bitmaporg = bitmapfactory.decoderesource( this .getcontext().getresources(), r.drawable.moon); matrix matrix = new matrix(); matrix.postrotate(- 90 ); //旋轉(zhuǎn)的角度 bitmap resizedbitmap = bitmap.createbitmap(bitmaporg, 0 , 0 , bitmaporg.getwidth(), bitmaporg.getheight(), matrix, true ); bitmapdrawable bmd = new bitmapdrawable(resizedbitmap); |
2、獲取手機(jī)號(hào)碼
1
2
3
4
5
6
7
8
9
10
|
//創(chuàng)建電話管理 telephonymanager tm = (telephonymanager) //與手機(jī)建立連接 activity.getsystemservice(context.telephony_service); //獲取手機(jī)號(hào)碼 string phoneid = tm.getline1number(); //記得在manifest file中添加 <uses-permission android:name= "android.permission.read_phone_state" /> //程序在模擬器上無(wú)法實(shí)現(xiàn),必須連接手機(jī) |
3.格式化string.xml 中的字符串
1
2
3
4
|
// 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設(shè)置全屏的方法
a.在java代碼中設(shè)置
1
2
3
4
|
/** 全屏設(shè)置,隱藏窗口所有裝飾 */ requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); |
b、在androidmanifest.xml中配置
1
2
3
4
5
6
7
|
<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、設(shè)置activity為dialog的形式
在androidmanifest.xml中配置activity節(jié)點(diǎn)是配置theme如下:
1
|
android:theme= "@android:style/theme.dialog" |
6、檢查當(dāng)前網(wǎng)絡(luò)是否連上
1
2
3
|
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 增加權(quán)限:
1
|
<uses-permission android:name= "android.permission.access_network_state" /> |
7、檢測(cè)某個(gè)intent是否有效
1
2
3
4
5
6
7
8
|
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 撥打電話
1
2
3
4
5
6
7
|
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中發(fā)送email
1
2
3
4
5
6
7
|
intent i = new intent(intent.action_send); //i.settype("text/plain"); //模擬器請(qǐng)使用這行 i.settype( "message/rfc822" ) ; // 真機(jī)上使用這行 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中打開瀏覽器
1
2
3
|
intent viewintent = new intent( "android.intent.action.view" ,uri.parse( "http://vaiyanzi.cnblogs.com" )); startactivity(viewintent); |
11、android 獲取設(shè)備唯一標(biāo)識(shí)碼
1
|
string android_id = secure.getstring(getcontext().getcontentresolver(), secure.android_id); |
12、android中獲取ip地址
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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獲取存儲(chǔ)卡路徑以及使用情況
1
2
3
4
5
6
7
8
9
10
|
/** 獲取存儲(chǔ)卡路徑 */ file sdcarddir=environment.getexternalstoragedirectory(); /** statfs 看文件系統(tǒng)空間使用情況 */ statfs statfs= new statfs(sdcarddir.getpath()); /** block 的 size*/ long blocksize=statfs.getblocksize(); /** 總 block 數(shù)量 */ long totalblocks=statfs.getblockcount(); /** 已使用的 block 數(shù)量 */ long availableblocks=statfs.getavailableblocks(); |
14 android中添加新的聯(lián)系人
1
2
3
4
5
6
7
8
9
10
11
|
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、查看電池使用情況
1
2
|
intent intentbatteryusage = new intent(intent.action_power_usage_summary); startactivity(intentbatteryusage); |
16、獲取進(jìn)程號(hào)
1
2
3
4
5
6
|
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); } |
希望本文所述對(duì)大家android程序設(shè)計(jì)有所幫助。