Android編程15個很有用的程式碼片段

來源:互聯網
上載者:User

Android代碼

1:查看是否有儲存卡插入string status=environment.getexternalstoragestate(); if(status.equals(enviroment.media_mounted)) {    說明有sd卡插入}

2:讓某個activity透明oncreate中不設layout this.settheme(r.style.theme_transparent); 以下是theme_transparent的定義(注意transparent_bg是一副透明的圖片)

3:在螢幕元素中設定控制代碼   使用activity.findviewbyid來取得螢幕上的元素的控制代碼. 使用該控制代碼您可以設定或擷取任何該對象外露的值.   textview msgtextview = (textview)findviewbyid(r.id.msg);   msgtextview.settext(r.string.push_me);

4:傳送簡訊             string body=”this is mms demo”;            intent mmsintent = new intent(intent.action_sendto, uri.fromparts(”smsto”, number, null));            mmsintent.putextra(messaging.key_action_sendto_message_body, body);            mmsintent.putextra(messaging.key_action_sendto_compose_mode, true);            mmsintent.putextra(messaging.key_action_sendto_exit_on_sent, true);             startactivity(mmsintent);

5:發送多媒體訊息            stringbuilder sb = new stringbuilder();             sb.append(”file://”);             sb.append(fd.getabsolutefile());             intent intent = new intent(intent.action_sendto, uri.fromparts(”mmsto”, number, null));             // below extra datas are all optional.             intent.putextra(messaging.key_action_sendto_message_subject, subject);             intent.putextra(messaging.key_action_sendto_message_body, body);             intent.putextra(messaging.key_action_sendto_content_uri, sb.tostring());             intent.putextra(messaging.key_action_sendto_compose_mode, composemode);             intent.putextra(messaging.key_action_sendto_exit_on_sent, exitonsent);             startactivity(intent);

7:發送mail              mime = “img/jpg”;             shareintent.setdataandtype(uri.fromfile(fd), mime);             shareintent.putextra(intent.extra_stream, uri.fromfile(fd));             shareintent.putextra(intent.extra_subject, subject);             shareintent.putextra(intent.extra_text, body);

8:註冊一個broadcastreceiver registerreceiver(mmasterresetreciever, new intentfilter(”oms.action.masterreset”)); private broadcastreceiver mmasterresetreciever = new broadcastreceiver() {         public void onreceive(context context, intent intent){             string action = intent.getaction();             if(”oms.action.masterreset”.equals(action)){                 recoverdefaultconfig();             }         }     };

9:定義contentobserver,監聽某個資料表private contentobserver mdownloadsobserver = new downloadschangeobserver(downloads.content_uri); private class downloadschangeobserver extends contentobserver {         public downloadschangeobserver(uri uri) {             super(new handler());         }         @override         public void onchange(boolean selfchange) {}          }   

10:獲得 手機ua public string getuseragent()     {            string user_agent = productproperties.get(productproperties.user_agent_key, null);             return user_agent;     }

11:清空手機上cookiecookiesyncmanager.createinstance(getapplicationcontext());         cookiemanager.getinstance().removeallcookie();

12:建立gprs串連    //dial the gprs link.     private boolean opendataconnection() {         // set up data connection.         dataconnection conn = dataconnection.getinstance();                 if (connectmode == 0) {                 ret = conn.openconnection(mcontext, “cmwap”, “cmwap”, “cmwap”);             } else {                 ret = conn.openconnection(mcontext, “cmnet”, “”, “”);             }     }

13:preferenceactivity 用法public class setting extends preferenceactivity {     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         addpreferencesfromresource(r.xml.settings);     } }setting.xml:             android:key=”seting2″             android:title=”@string/seting2″             android:summary=”@string/seting2″/>             android:key=”seting1″             android:title=”@string/seting1″             android:summaryoff=”@string/seting1summaryoff”             android:summaryon=”@stringseting1summaryoff”/>

14:通過httpclient從指定server擷取資料              defaulthttpclient httpclient = new defaulthttpclient();             httpget method = new httpget(“http://www.baidu.com/1.html”);             httpresponse resp;             reader reader = null;             try {                 // allclientpnames.timeout                 httpparams params = new basichttpparams();                 params.setintparameter(allclientpnames.connection_timeout, 10000);                 httpclient.setparams(params);                 resp = httpclient.execute(method);                 int status = resp.getstatusline().getstatuscode();                 if (status != httpstatus.sc_ok) return false;                 // httpstatus.sc_ok;                 return true;             } catch (clientprotocolexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } finally {                 if (reader != null) try {                     reader.close();                 } catch (ioexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }             }

15:顯示toast toast.maketext(this._getapplicationcontext(), r.string._item, toast.length_short).show();    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.