Android Practical Code Snippets Common code summary _android

Source: Internet
Author: User

1: Check to see if a memory card is plugged in

Copy Code code as follows:

String status=environment.getexternalstoragestate ();
if (Status.equals (enviroment.media_mounted))
{
Description has SD card insert
}

2: Make an activity transparent

Layout This.settheme (r.style.theme_transparent) is not set in OnCreate;
The following is the definition of theme_transparent (note that TRANSPARENT_BG is a transparent picture)


3: Set handle in Screen element

Use Activity.findviewbyid to get a handle to the element on the screen. Using this handle you can set or get any values that the object exposes.

Copy Code code as follows:

TextView Msgtextview = (TextView) Findviewbyid (r.id.msg);
Msgtextview.settext (R.string.push_me);

4: Send SMS

Copy Code code as follows:

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

Copy Code code as follows:

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)

6: Send mail

Copy Code code as follows:

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);

7: Register a Broadcastreceiver

Copy Code code as follows:

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 ();
}
}
}

8: Define Contentobserver, monitor a data table

Copy Code code as follows:

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) {}
}

9: Get Mobile UA

Copy Code code as follows:

Public String getuseragent ()
{
String user_agent = productproperties.get (Productproperties.user_agent_key, NULL);
return user_agent;
}

10: Empty the phone cookies

Copy Code code as follows:

Cookiesyncmanager.createinstance (Getapplicationcontext ());
Cookiemanager.getinstance (). Removeallcookie (); 11: Establish 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", "Cmwap", "Cmwap");
} else {
ret = conn.openconnection (Mcontext, "cmnet", "", "");
}
}

12:preferenceactivity usage

Copy Code code as follows:

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"/>

13: Get data from specified server via httpclient

Copy Code code as follows:

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 ();
}
}

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.