14 useful code snippets for Android programming

Source: Internet
Author: User
Tags transparent image

: Check whether a memory card is inserted.

  1. String status = environment. getexternalstoragestate ();
  2. If (status. Equals (enviroment. media_mounted ))
  3. {
  4. SD card insertion
  5. }

Copy code

2: Make an activity transparent

  1. Layout is not set in oncreate.
  2. This. settheme (R. style. theme_transparent );
  3. The following is the definition of theme_transparent (note that transparent_bg is a transparent image)

Copy code

3: Set the handle in the screen element
Use activity. findviewbyid to obtain the handle of the elements on the screen. With this handle, you can set or obtain any value exposed to this object.

  1. Textview msgtextview = (textview) findviewbyid (R. Id. msg );
  2. Msgtextview. settext (R. String. push_me );

Copy code

4: send SMS

  1. String body = "this is MMS Demo ";
  2. Intent mmsintent = new intent (intent. action_sendto, Uri. fromparts ("smsto", number, null ));
  3. Mmsintent. putextra (messaging. key_action_sendto_message_body, body );
  4. Mmsintent. putextra (messaging. key_action_sendto_compose_mode, true );
  5. Mmsintent. putextra (messaging. key_action_sendto_exit_on_sent, true );
  6. Startactivity (mmsintent );

Copy code

5. Send MMS messages

  1. Stringbuilder sb = new stringbuilder ();
  2. SB. append ("file ://");
  3. SB. append (FD. getabsolutefile ());
  4. Intent intent = new intent (intent. action_sendto, Uri. fromparts ("mmsto", number, null ));
  5. // Below extra datas are all optional.
  6. Intent. putextra (messaging. key_action_sendto_message_subject, subject );
  7. Intent. putextra (messaging. key_action_sendto_message_body, body );
  8. Intent. putextra (messaging. key_action_sendto_content_uri, SB. tostring ());
  9. Intent. putextra (messaging. key_action_sendto_compose_mode, composemode );
  10. Intent. putextra (messaging. key_action_sendto_exit_on_sent, exitonsent );
  11. Startactivity (intent );

Copy code

6: Display toast

  1. Toast. maketext (this. _ getapplicationcontext (), R. String. _ item, Toast. length_short). Show ();

Copy code

7: Send mail

  1. Mime = "img/jpg ";
  2. Using intent. setdataandtype (URI. fromfile (FD), mime );
  3. Extends intent. putextra (intent. extra_stream, Uri. fromfile (FD ));
  4. Specify intent. putextra (intent. extra_subject, subject );
  5. Extends intent. putextra (intent. extra_text, body );

Copy code

8: register a broadcastreceiver

  1. Registerreceiver (mmasterresetreciever, new intentfilter ("OMS. Action. masterreset "));
  2. Private broadcastreceiver mmasterresetreciever = new broadcastreceiver (){
  3. Public void onreceive (context, intent ){
  4. String action = intent. getaction ();
  5. If ("OMS. Action. masterreset". Equals (Action )){
  6. Recoverdefaconfig config ();
  7. }
  8. }
  9. };

Copy code

9: Define contentobserver to listen to a data table

  1. Private contentobserver mdownloadsobserver = new downloadschangeobserver (downloads. content_uri );
  2. Private class downloadschangeobserver extends contentobserver {
  3. Public downloadschangeobserver (URI ){
  4. Super (new handler ());
  5. }
  6. @ Override
  7. Public void onchange (Boolean selfchange ){}
  8. }

Copy code

10: Get the UA

  1. Public String getuseragent ()
  2. {
  3. String user_agent = productproperties. Get (productproperties. user_agent_key, null );
  4. Return user_agent;
  5. }

Copy code

11: Clear cookies on mobile phones

  1. Cookiesyncmanager. createinstance (getapplicationcontext ());
  2. Cookiemanager. getinstance (). removeallcookie ();

Copy code

12. Establish a GPRS connection

  1. // Dial the GPRS link.
  2. Private Boolean opendataconnection (){
  3. // Set up data connection.
  4. Dataconnection conn = dataconnection. getinstance ();
  5. If (connectmode = 0 ){
  6. Ret = conn. openconnection (mcontext, "cmwap ");
  7. } Else {
  8. Ret = conn. openconnection (mcontext, "cmnet", "", "");
  9. }
  10. }

Copy code

13: preferenceactivity usage

  1. Public class setting extends preferenceactivity
  2. {
  3. Public void oncreate (bundle savedinstancestate ){
  4. Super. oncreate (savedinstancestate );
  5. Addpreferencesfromresource (R. xml. settings );
  6. }
  7. }

Copy code

Setting. xml:

  1. Android: Key = "seting2 ″
  2. Android: Title = "@ string/seting2 ″
  3. Android: Summary = "@ string/seting2"/>
  4. Android: Key = "seting1 ″
  5. Android: Title = "@ string/seting1 ″
  6. Android: summaryoff = "@ string/seting1summaryoff"
  7. Android: summaryon = "@ stringseting1summaryoff"/>

Copy code

14. Use httpclient to obtain data from a specified server

  1. Defaulthttpclient httpclient = new defaulthttpclient ();
  2. Httpget method = new httpget ("http://www.baidu.com/1.html ");
  3. Httpresponse resp;
  4. Reader reader = NULL;
  5. Try {
  6. // Allclientpnames. Timeout
  7. Httpparams Params = new basichttpparams ();
  8. Params. setintparameter (allclientpnames. connection_timeout, 10000 );
  9. Httpclient. setparams (Params );
  10. Resp = httpclient.exe cute (method );
  11. Int status = resp. getstatusline (). getstatuscode ();
  12. If (status! = Httpstatus. SC _ OK) return false;
  13. // Httpstatus. SC _ OK;
  14. Return true;
  15. } Catch (clientprotocolexception e ){
  16. // Todo auto-generated Catch Block
  17. E. printstacktrace ();
  18. } Catch (ioexception e ){
  19. // Todo auto-generated Catch Block
  20. E. printstacktrace ();
  21. } Finally {
  22. If (reader! = NULL) Try {
  23. Reader. Close ();
  24. } Catch (ioexception e ){
  25. // Todo auto-generated Catch Block
  26. E. printstacktrace ();
  27. }
  28. }

Copy code

 

Related Article

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.