转载声明:共同学习,Android
转载地址:http://hi.baidu.com/gaogaf/blog/item/77dc552c3cedd7fd8a139937.html
Android编程15个很有用的代码片段
2010年12月10日 星期五 下午 01:14
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:清空手机上Cookie
CookieSyncManager.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编程14个很有用的代码片段
7.7.5 有用的代码片段 7.7.6 启用纹理 7.7.7 综合示例 7.7.8 texture类 7.8 索引顶点:重用是有好处的 7.8.1 代码整合 7.8.2 vertices类 7.9 半透明混合处理 7.10 更多图元:点、线、条和扇 ...
Android4高级编程(第三版)源码,包含书中的示例代码片段和完整的项目源码
android-snippets 这个 repo 只是各种 Android 代码片段的集合。 ####Android Studio IDE ####地图和位置 ####应用间通信 从另一个应用程序启动应用程序活动
7.7.5 有用的代码片段 251 7.7.6 启用纹理 251 7.7.7 综合示例 251 7.7.8 Texture类 253 7.8 索引顶点:重用是有好处的 255 7.8.1 代码整合 256 7.8.2 Vertices类 258 7.9 半透明混合处理 260 ...
15个Android很有用的代码片段.pdf 3G应用开发之Android.ppt Android 图像处理篇.rar Android2.2+API+中文文档系列.pdf Android_CTS测试研究.pdf Android_GoogleMaps基础开发教程.pdf Android_GPS架构分析.doc ...
15个Android很有用的代码片段、Android_GPS架构分析、android_JNI编程_一些技巧、Android_NDK开发实例、Android_RIL层剖析(官方翻译)、Android的Message机制(简单小结)、Android的主题和风格介绍、Android日历完整...
根据《 Android编程权威指南》第一版学习Android的记录。 使用AndroidStudio及Android 4.0进行最低支持Android 4.0根据新特性做了相应修改 GeoQuiz 第一个应用,通过它学习Android应用的基本组成,活动,界面布局...
在这里,我们针对移动开发人员列出了9个非常有用的Android代码编辑器。 TOP1:Code Peeker Code Peeker可以让语法高亮显示并允许你查找代码片段,且被Google Play选中作为教育项目的一部分。Code Peeker可以打开和...
C++ Socket网络编程大全,里面都是一些网络编程的代码片段,很实用!!
有一些完整的UI设计的屏幕截图,并且还上传了UI的一个gif文件,从那里我想到了在Android编程中实现它的想法。 使用步骤... 启动应用程序,登录页面将出现第一个屏幕。 您必须填写“电子邮件和密码”,然后“登录...
这个文件夹包含我对 Android 的实验。这将涵盖从简单到复杂的 android 开发教程和实验。 如果您遇到任何麻烦,可以给我发电子邮件:-> 快乐的安卓编程! ;)
Android音乐编辑软件:音乐快剪源代码,一个基于Android系统的铃声剪切软件。使用这个小应用你可以将手机上的MP3或录音片段重新裁切合成,或者用于制作手机铃声,当然功能还不至这些,这是音乐快剪早期的源代码,和...
Dash for mac是使用与Mac OS平台的软件编程文档管理工具,可以浏览API文档,以及管理代码片段工具。Dash自带了丰富的API文档,涉及各种主流的编程语言和框架。 可以浏览API文档,以及管理代码片段工具。Dash自带了...
Dash 是适用于Mac OS平台的软件编程文档管理工具,可以浏览API文档,以及管理代码片段工具。Dash自带了丰富的API文档,涉及各种主流的编程语言和框架。 通过Dash可以浏览API文档,以及管理代码片段工具。Dash自带了...
Android外壳 ... 例如:此代码片段仅是ss卡,然后将结果返回到变量中。 try { // Executes the command. Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard"); // Re
通过Dash可以浏览API...利用Dash的代码片段管理功能,你可以把日常使用频繁的代码保存起来,然后为其设置一个独一无二的缩写,这样一来原本需要一遍又一遍的敲击键盘重复录入的繁琐工作,就可以交给Dash来轻松搞定。