: Check whether a memory card is inserted.
- String status = environment. getexternalstoragestate ();
- If (status. Equals (enviroment. media_mounted ))
- {
- SD card insertion
- }
Copy code
2: Make an activity transparent
- Layout is not set in oncreate.
- This. settheme (R. style. theme_transparent );
- 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.
- Textview msgtextview = (textview) findviewbyid (R. Id. msg );
- Msgtextview. settext (R. String. push_me );
Copy code
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 );
Copy code
5. Send MMS messages
- 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 );
Copy code
6: Display toast
- Toast. maketext (this. _ getapplicationcontext (), R. String. _ item, Toast. length_short). Show ();
Copy code
7: Send mail
- Mime = "img/jpg ";
- Using intent. setdataandtype (URI. fromfile (FD), mime );
- Extends intent. putextra (intent. extra_stream, Uri. fromfile (FD ));
- Specify intent. putextra (intent. extra_subject, subject );
- Extends intent. putextra (intent. extra_text, body );
Copy code
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 ();
- }
- }
- };
Copy code
9: Define contentobserver to 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 ){}
- }
Copy code
10: Get the UA
- Public String getuseragent ()
- {
- String user_agent = productproperties. Get (productproperties. user_agent_key, null );
- Return user_agent;
- }
Copy code
11: Clear cookies on mobile phones
- Cookiesyncmanager. createinstance (getapplicationcontext ());
- Cookiemanager. getinstance (). removeallcookie ();
Copy code
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", "", "");
- }
- }
Copy code
13: preferenceactivity usage
- Public class setting extends preferenceactivity
- {
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Addpreferencesfromresource (R. xml. settings );
- }
- }
Copy code
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"/>
Copy code
14. Use httpclient to obtain data from a specified 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.exe cute (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 ();
- }
- }
Copy code