转--Android实用的代码片段 常用代码总结
1:查看是否有存储卡插入
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)findViewById(R.id.msg);
msgTextView.setText(R.string.push_me);
4:发送短信
代码如下:
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.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
代码如下:
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
代码如下:
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,监听某个数据表
代码如下:
= 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
代码如下:
{
String user_agent =
ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);
return user_agent;
}
10:清空手机上Cookie
代码如下:
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 用法
代码如下:
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();
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();
}
}
转--Android实用的代码片段 常用代码总结的更多相关文章
- Android开发java程序员常用代码,将字符串以逗号分别取出字符串String
public class StringSplit { public static void main(String[] args) { String sourceStr = "1,2,3,4 ...
- 在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途
contex指上下文,实际上就是一个web项目:path是虚拟目录,访问的时候用127.0.0.1:8080/welcom/*.jsp访问网页,welcome前面要加/;docBase是网页实际存放位 ...
- VS代码片段(snippet)创作工具——Snippet Editor(转)
原文:http://blog.csdn.net/oyi319/article/details/5605502 从Visual Studio 2005开始,IDE支持代码片段.代码片段以代码缩写和TAB ...
- sublimetext3中保存代码片段
在日常的开发工作中,不断重复上一次敲过的代码,有时确实感到伐木累."蓝瘦"(难受)."香菇"(想哭),大概表达的也是这样的心境吧!:grinning: 所以,在 ...
- SQL Prompt自定义代码片段
新增代码片段: 代码片段管理: 代码片段中可以使用以下占位符:详见参考: $DATE$ 插入当前日期. $TIME$ 插入当前时间 $USER$ 插入当前电脑的用户名 $PASTE$ 插入剪切板内容 ...
- Sublime Text3—Code Snippets(自定义代码片段)
摘要 程序员总是会不断的重复写一些简单的代码片段,为了提高编码效率,我们可以把经常用到的代码保存起来再调用. 平时用sublime安装各种插件,使用Tab键快速补全,便是snippets(可译为代码片 ...
- VS Code 折腾记 - (6) 基本配置/快捷键定义/代码片段的录入(snippet)
前言 本来分成三篇来写的,但是想了想没必要,大家都是聪明人...简单的东西点一下就晓得了. 基本配置 快捷键自定义(Ctrl+K Ctrl + S) 那个when支持条件表达式返回一个布尔值 支持的快 ...
- 微信小程序代码片段
微信小程序代码片段是一种可分享的小项目,可用于分享小程序和小游戏的开发经验.展示组件和 API 的使用.复现开发问题等等.分享代码片段会得到一个链接,所有拥有此分享链接的人可以在工具中导入此代码片段. ...
- 如何折叠IntelliJ IDEA代码片段
在 IntelliJ IDEA 中您可以折叠(fold)代码片段,将它们缩小到单个可见行.这样,您可以隐藏当前看起来无关紧要的细节.如果必要的话,折叠的代码片段可以被展开(unfolded). 折 ...
随机推荐
- JS 的线程、事件循环、任务队列简介
JS 是单线程的,但是却能执行异步任务,这主要是因为 JS 中存在事件循环(Event Loop)和任务队列(Task Queue). 事件循环:JS 会创建一个类似于 while (true) 的循 ...
- STL 简介,标准模板库
这篇文章是关于C++语言的一个新的扩展--标准模板库的(Standard Template Library),也叫STL. 当我第一次打算写一篇关于STL的文章的时候,我不得不承认我当时低估了这个话 ...
- 查找目录下的所有文件中是否含有某个字符串 linux
查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...
- java Pattern
public class Test{ //匹配替换掉order by之后的字符串 public static void main(String[] args) { Pattern pattern = ...
- HTML和JSON的数据交互-jsonp跨域
HTML和json的数据交互 <!DOCTYPE html> <html> <head> <script src="//ajax.googleapi ...
- JSP Request方法大全
协议:request.getProtocol() 输出:HTTP/1.1 服务器信息 getServletConfig().getServletContext().getServerInfo() 输出 ...
- maxsdk sample中3dsexp.rc点不开并提示specstrings.h中找不到sal.h解法
在网上下载sal.h文件并拷贝到specstrings.h所在目录(C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include)即可. sa ...
- Python报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)
解决办法: 在报错的页面添加代码: import sys reload(sys) sys.setdefaultencoding('utf8')
- Sql2008 全文索引应用(错误7625)
在SQL Server 中提供了一种名为全文索引的技术,可以大大提高从长字符串里搜索数 据的速度,不用在用LIKE这样低效率的模糊查询了. 下面简明的介绍如何使用Sql2008 全文索引 一.检查服务 ...
- Bootstrap 3兼容IE8浏览器(转)
Bootstrap是一个响应式的布局,你可以在宽屏电脑.普通电脑,平板电脑,手机上都得到非常优秀的布局体验.这种响应式的布局正是通过CSS3的媒体查询(Media Query)功能实现的,根据不同的分 ...