15 useful code snippets for Android programming

Source: Internet
Author: User

Android code

1: Check whether there is a memory card inserted string status = environment. getexternalstoragestate (); if (status. equals (enviroment. media_mounted) {Note that there is an SD card inserted}

2: Set layout this. settheme (r. style. theme_transparent) to transparent oncreate of an activity. The following is the definition of theme_transparent (note that transparent_bg is a transparent image)

3: Set the handle to use activity in the screen element. findviewbyid to get the handle of the elements on the screen. with this handle, you can set or obtain any exposed value of this object. textview msgtextview = (textview) findviewbyid (r. id. msg); msgtextview. settext (r. string. push_me );

4: send SMS 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: Send MMS stringbuilder sb = new stringbuilder (); sb. append ("file: //"); sb. append (fd. getabsolutefile (); 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: Send mail mime = "img/jpg"; upload intent. setdataandtype (uri. fromfile (fd), mime); Specify intent. putextra (intent. extra_stream, uri. fromfile (fd); Specify intent. putextra (intent. extra_subject, subject); Specify intent. putextra (intent. extra_text, body );

8: register a broadcastreceiver registerreceiver (mmasterresetreciever, new intentfilter ("oms. action. masterreset "); private broadcastreceiver mmasterresetreciever = new broadcastreceiver () {public void onreceive (context, intent) {string action = intent. getaction (); if ("oms. action. masterreset ". equals (action) {recoverdefaconfig config ();}}};

9: Define contentobserver and listen to a data table private contentobserver mdownloadsobserver = new downloadschangeobserver (downloads. content_uri); private class downloadschangeobserver extends contentobserver {public downloadschangeobserver (uri) {super (new handler ();} @ override public void onchange (boolean selfchange ){}}

10: Obtain the ua public string getuseragent () {string user_agent = productproperties. get (productproperties. user_agent_key, null); return user_agent ;}

11: Clear cookiecookiesyncmanager. createinstance (getapplicationcontext (); cookiemanager. getinstance (). removeallcookie ();

12: Establish a gprs connection // dial the gprs link. private boolean opendataconnection () {// set up data connection. dataconnection conn = dataconnection. getinstance (); if (connectmode = 0) {ret = conn. openconnection (mcontext, "cmwap");} else {ret = conn. openconnection (mcontext, "cmnet", "", "") ;}}

13: preferenceactivity usage 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: Use httpclient to obtain data from the specified server: defaulthttpclient httpclient = new defaulthttpclient (); httpget method = new httpget ("Success); httpresponse resp; reader = null; try {// allclientpnames. timeout httpparams params = new basichttpparams (); params. setintparameter (allclientpnames. connection_time out, 10000); httpclient. setparams (params); resp = httpclient.exe cute (method); int st Atus = 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: Display toast. maketext (this. _ getapplicationcontext (), r. string. _ item, toast. length_short). show ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.