Android 代码片段
来源:互联网 发布:手机淘宝怎样用优惠券 编辑:程序博客网 时间:2024/05/21 01:50
Android代码
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);
7:发送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);
8:注册一个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(); } } };
9:定义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) {} }
10:获得 手机ua public string getuseragent() { string user_agent = productproperties.get(productproperties.user_agent_key, null); return user_agent; }
11:清空手机上cookiecookiesyncmanager.createinstance(getapplicationcontext()); cookiemanager.getinstance().removeallcookie();
12:建立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”, “”, “”); } }
13: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”/>
14:通过httpclient从指定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.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(); } }
15:显示toast toast.maketext(this._getapplicationcontext(), r.string._item, toast.length_short).show();
- [代码片段] Android实用代码片段整合
- android常用代码片段
- android 代码片段
- Android 代码片段
- android有用代码片段
- android常用代码片段
- android有用代码片段
- android有用代码片段
- android有用代码片段
- Android实用代码片段
- android常用代码片段
- Android代码片段
- Android常用代码片段
- Android 代码片段
- android 代码片段
- Android有用代码片段
- Android 常用代码片段
- android实用代码片段
- 指针的学习.
- WAS下org.xml.sax.SAXParseException: cvc-elt.1:找不到元素“beans”的声明
- 简单去掉 优酷,土豆,酷六等热门视频网站 播放前的 15秒广告
- 浏览器兼容的css阴影效果
- Android Webview实现文件下载功能
- Android 代码片段
- websphere6发布新的应用程序:Could not load resource "META-INF/application.xml"
- 火星人敏捷开发手册 原10.31版本已于10.14提前发布,特此通知
- win7伪神key
- 仅通过崩溃地址找出源代码的出错行
- ARC error when compiling
- 所有排序方法之外的排序方法
- URL工具类
- ajax4jsf常用标签的使用