这篇文章主要介绍了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. Mac上因磁盘格式导致gulp无限刷新问题

    今天遇到个超奇葩的问题,使用gulp.watch监控文件变化,但是并没有修改文件,却一直执行change,导致浏览器无限刷新 调试了10小时,代码各种删改,一直不得其解.切换到Windows运行,又正 ...

  2. Android TextView换行问题

    本文转载于:http://niufc.iteye.com/blog/1729792 ndroid的TextView在显示文字的时候有个问题就是一行还没显示满就跳到下一行,原因是: 1) TextVie ...

  3. VIM进阶学习之几种模式和按键映射

    Map是Vim强大的一个重要原因,可以自定义各种快捷键,用起来自然得心应手. vim里最基本的map用法也就是 :map c a 这里把c映射成了a,在map生效的情况下,按下c就等同于按下了a 当然 ...

  4. Resource Manager

    Azure Resource Manager overview https://azure.microsoft.com/en-us/documentation/articles/resource-gr ...

  5. MiniCRT 64位 linux 系统移植记录:64位gcc的几点注意

    32位未修改源码与修改版的代码下载: git clone git@github.com:youzhonghui/MiniCRT.git MiniCRT 64位 linux 系统移植记录 MiniCRT ...

  6. asm单机dg dbca报错ORA-01031 CRS-2676,rman restore主库控制文件报错ORA-15081

    dg-> ll $ORACLE_HOME/bin/oracle -r-xr-s--x 1 oracle asmadmin 210824714 Nov 20 16:41 /u01/app/orac ...

  7. 写EXCEL(csv 可以用EXECEL打开,逗号分列隔符)

    FILE *file = NULL; char path[]="D:\\Data\\Pos.csv"; CTime m_tDateTime; m_tDateTime = m_tDa ...

  8. JavaScript的事件绑定及深入

     事件绑定分为两种:一种是传统事件绑定(内联模型,脚本模型),一种是现代事件绑定 (DOM2 级模型).现代事件绑定在传统绑定上提供了更强大更方便的功能. 一.传统事件绑定的问题传统事件绑定有内联模型 ...

  9. AR、美颜、机器人:计算机视觉库几乎无所不在

    最近日本推出的反美颜应用Primo可能让感到不胜惶恐.其实,这样反人类的应用,你也能写出,不过必须了解的一些技术,就是计算机视觉.目前,计算机视觉库包括FastCV.OpenCV.JavaCV等. 相 ...

  10. 怎么用ABBYY打开PDF文档

    我们日常工作中接触的文档大多都是PDF格式的,这种格式的文件需要借助工具才能打开,大家最熟悉的无非就是Adobe了,但你知道吗?除了Adobe,OCR文字识别软件也可以打开PDF文档,比如ABBYY ...