这篇文章主要介绍了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)

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("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实用的代码片段 常用代码总结的更多相关文章

  1. Android开发java程序员常用代码,将字符串以逗号分别取出字符串String

    public class StringSplit { public static void main(String[] args) { String sourceStr = "1,2,3,4 ...

  2. 在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

    contex指上下文,实际上就是一个web项目:path是虚拟目录,访问的时候用127.0.0.1:8080/welcom/*.jsp访问网页,welcome前面要加/;docBase是网页实际存放位 ...

  3. VS代码片段(snippet)创作工具——Snippet Editor(转)

    原文:http://blog.csdn.net/oyi319/article/details/5605502 从Visual Studio 2005开始,IDE支持代码片段.代码片段以代码缩写和TAB ...

  4. sublimetext3中保存代码片段

    在日常的开发工作中,不断重复上一次敲过的代码,有时确实感到伐木累."蓝瘦"(难受)."香菇"(想哭),大概表达的也是这样的心境吧!:grinning: 所以,在 ...

  5. SQL Prompt自定义代码片段

    新增代码片段: 代码片段管理: 代码片段中可以使用以下占位符:详见参考: $DATE$ 插入当前日期. $TIME$ 插入当前时间 $USER$ 插入当前电脑的用户名 $PASTE$ 插入剪切板内容 ...

  6. Sublime Text3—Code Snippets(自定义代码片段)

    摘要 程序员总是会不断的重复写一些简单的代码片段,为了提高编码效率,我们可以把经常用到的代码保存起来再调用. 平时用sublime安装各种插件,使用Tab键快速补全,便是snippets(可译为代码片 ...

  7. VS Code 折腾记 - (6) 基本配置/快捷键定义/代码片段的录入(snippet)

    前言 本来分成三篇来写的,但是想了想没必要,大家都是聪明人...简单的东西点一下就晓得了. 基本配置 快捷键自定义(Ctrl+K Ctrl + S) 那个when支持条件表达式返回一个布尔值 支持的快 ...

  8. 微信小程序代码片段

    微信小程序代码片段是一种可分享的小项目,可用于分享小程序和小游戏的开发经验.展示组件和 API 的使用.复现开发问题等等.分享代码片段会得到一个链接,所有拥有此分享链接的人可以在工具中导入此代码片段. ...

  9. 如何折叠IntelliJ IDEA代码片段

      在 IntelliJ IDEA 中您可以折叠(fold)代码片段,将它们缩小到单个可见行.这样,您可以隐藏当前看起来无关紧要的细节.如果必要的话,折叠的代码片段可以被展开(unfolded). 折 ...

随机推荐

  1. 使用CURL下载远程文件保存到服务器

    比如微信公众平台开发,下载用户的头像到服务器上: /** * 使用CURL获取远程文件保存到服务器 *@param $image=$oJSON->headimgurl; 获取到的微信返回的头像U ...

  2. (转)现代C++函数式编程

    本文转自:http://geek.csdn.net/news/detail/96636     现代C++函数式编程 C++ 函数式编程 pipeline 开发经验 柯里化 阅读2127    作者简 ...

  3. js获取ifram对象

    1.获取iframe对象 var doc=document.getElementById('frameId').contentWindow.document; //var doc=parent.doc ...

  4. Windows下IntelliJ IDEA中运行Spark Standalone

    ZHUAN http://www.cnblogs.com/one--way/archive/2016/08/29/5818989.html http://www.cnblogs.com/one--wa ...

  5. __LINE__ __DATE__ __FILE__ __TIME__ 等宏定义解释

    Names the predefined ANSI C and Microsoft C++ implementation macros. The compiler recognizes predefi ...

  6. Sql Server 事务隔离级别的查看及更改

    根据自身 Sql Server 的情况来自定义 事务隔离级别,将会更加的满足需求,或提升性能.例如,对于逻辑简单的 Sql Server,完全可以使用 read uncommitted 模式,来减少死 ...

  7. OSI安全体系结构

    建立七层模型主要是为解决异种网络互连时所遇到的兼容性问题.它的最大优点是将服务.接口和协议这三个概念明确地区分开来;也使网络的不同功能模块分担起 不同的职责.也就是说初衷在于解决兼容性,但当网络发展到 ...

  8. 递归函数练习:输出菲波拉契(Fibonacci)数列的前N项数据

    /*====================================================================== 著名的菲波拉契(Fibonacci)数列,其第一项为0 ...

  9. Android 广播大全 Intent Action 事件

    Intent.ACTION_AIRPLANE_MODE_CHANGED; //关闭或打开飞行模式时的广播 Intent.ACTION_BATTERY_CHANGED; //充电状态,或者电池的电量发生 ...

  10. sqlserver快照,启用基于行版本控制的隔离级别

    在sqlserver标准的已提交读(read committed)隔离级别下,读写操作相互阻塞.未提交读(read uncommitted)虽然不会有这种阻塞,但是读操作可能会读到脏数据,这是大部分用 ...