欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实用的代码片段 常用代码总结

程序员文章站 2022-04-30 23:18:19
1:查看是否有存储卡插入 复制代码 代码如下: string status=environment.getexternalstoragestate(); if(sta...

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)

6:发送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);

7:注册一个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();
            }
        }
    }

8:定义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) {} 
        }

9:获得 手机ua

复制代码 代码如下:

public string getuseragent()
    {
           string user_agent = productproperties.get(productproperties.user_agent_key, null);
            return user_agent;
    }

10:清空手机上cookie

复制代码 代码如下:

cookiesyncmanager.createinstance(getapplicationcontext());
        cookiemanager.getinstance().removeallcookie();11:建立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", "", "");
            }
    }

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

13:通过httpclient从指定server获取数据

复制代码 代码如下:

defaulthttpclient httpclient = new defaulthttpclient();
            httpget method = new httpget("");
            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();
                }
            }