Android很有用的代码片段
1:查看是否有存储卡插入
-
- String status=Environment.getExternalStorageState();
-
if(status.equals(Enviroment.MEDIA_MOUNTED))
- {
- 说明有SD卡插入
- }
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);
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);
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);
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);
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();
- }
- }
- };
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) {}
- }
-
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;
- }
public String getUserAgent()
{
String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);
return user_agent;
}
10:清空手机上Cookie
- CookieSyncManager.createInstance(getApplicationContext());
- CookieManager.getInstance().removeAllCookie();
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”, “”, “”);
- }
- }
//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”/>
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();
- }
- }
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();
}
}
14:显示toast
- String status=Environment.getExternalStorageState();
- if(status.equals(Enviroment.MEDIA_MOUNTED))
- {
- 说明有SD卡插入
- }
- TextView msgTextView = (TextView)findViewById(R.id.msg);
- msgTextView.setText(R.string.push_me);
- 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);
- 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);
- 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);
- 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();
- }
- }
- };
- 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) {}
- }
- public String getUserAgent()
- {
- String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);
- return user_agent;
- }
- CookieSyncManager.createInstance(getApplicationContext());
- CookieManager.getInstance().removeAllCookie();
- //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”, “”, “”);
- }
- }
- 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”/>
- 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开发中,代码片段(Code Snippet)是开发者日常工作中不可或缺的一部分,它们通常是解决特定问题或实现某一功能的小段代码。这些代码片段可以大大提高开发效率,减少重复工作,并且帮助初学者理解复杂的API...
在Android开发中,掌握一些实用的代码片段可以显著提高开发效率和代码质量。以下是对"20个Android很有用的代码片段"这个主题的详细解释,涵盖了这些代码片段可能涉及的关键知识点。 1. **异步操作**:Android UI...
Android代码片段文档,如"Android代码速查.doc"和"Android有用的代码片段.doc",以及"Android小知识整理.rar"和"Android代码片段.txt",将涵盖这些领域的实用代码,对于初学者来说是极好的学习资源。通过深入研究和...
在Android开发过程中,...这些代码片段涵盖了Android开发中的多个方面,包括UI交互、数据管理、性能优化和用户体验提升,对于Android开发者来说非常实用。掌握并灵活运用这些技巧,将有助于提升你的Android开发技能。
在Android开发过程中,掌握一些实用的代码片段和技巧可以极大地提高开发效率和代码质量。以下是一些关键点的详细说明: 1. **检测SD卡状态**: 可以通过`Environment.getExternalStorageState()`方法来检查SD卡...
【Android 代码片段整理】 在Android开发中,经常会遇到...这些代码片段是Android开发中常见的实用工具函数,对于构建功能完备的应用程序至关重要。理解和掌握这些方法,可以帮助开发者更高效地完成日常的开发工作。
这本书的目的是帮助读者快速掌握Android开发的基础,并提供一系列实用的代码片段,以便在实际项目中进行应用。 1. **Android SDK**:Android软件开发工具包是开发Android应用的基础,它包含了编写、调试和运行...
在Android开发中,掌握一些实用的代码片段能够极大地提高开发效率和代码质量。以下是对给定内容的详细解释和扩展: 1. **检查SD卡状态**: Android提供了`Environment.getExternalStorageState()`方法来获取SD卡的...
在Android开发领域,掌握一些常用的代码片段是提升开发效率的关键。这份名为"【免费第一弹】android常用代码大全!"的资源集锦,显然是开发者们的福音。它包含了各种实用的Android代码示例,旨在帮助开发者解决日常...
"Android一些模板代码"这个压缩包很可能包含了一系列常用的Android开发代码片段,涵盖诸如Activity、Fragment、Adapter、网络请求、数据库操作、权限管理等各个方面。下面将对这些常见知识点进行详细解释。 1. **...
这个库通常包含了各种常用的代码片段和助手类,使得开发者在处理常见任务时能够快速地找到解决方案,避免重复造轮子。 **主要功能模块** 1. **数据处理**:Android Util可能包含对数据操作的支持,如JSON解析、...
总的来说,AndroidNavigation是一个强大的工具,能够帮助开发者更高效地处理Android应用中的导航和界面管理问题,特别是涉及嵌套片段、状态栏和工具栏的场景。通过利用这个开源库,开发者可以专注于业务逻辑的实现,...
### Android 20个常用的系统调用代码片段详解 #### 1. 从Google搜索内容 ```java Intent intent = new Intent();...开发者可以通过灵活运用这些代码片段来实现更多实用的功能,提高应用程序的用户体验。
通过上述分析可以看出,《Android轻松实现语音识别的完整代码》项目为开发者提供了一个简洁而实用的例子,展示了如何在Android应用中集成语音识别功能。这对于希望利用语音交互增强用户体验的开发者来说是一份宝贵的...
【Android开发人员不得不收集的代码】是一份针对Android开发者的重要资源集合,涵盖了各种实用的代码片段和工具类,旨在提高开发效率和优化代码质量。这个压缩包中的项目名为"Blankj-AndroidUtilCode-0edc62e",表明...
这个标题暗示我们将探讨uni-app中的实用代码片段和基础组件,这些都是构建高效跨平台应用的关键元素。 一、uni-app核心概念 1. 组件化:uni-app遵循Vue.js的组件化思想,每个UI元素或功能模块都可以封装为一个组件...
2. **代码自动完成**:提供智能代码补全功能,根据上下文自动提示可能的代码片段,加快编码速度。 3. **代码折叠**:允许用户折叠和展开代码块,便于管理和阅读复杂的代码结构。 4. **查找与替换**:内置搜索功能...
在Android应用中,这种功能常用于教程、示例或者开发者社区,以便用户更清晰地查看和学习代码片段。 2. **自定义View**:在Android中,开发者可以通过继承`View`类或者`TextView`类来自定义自己的视图组件。在这个...
- `原创15个Android很有用的代码片段.pdf`:这份文档包含了一些实用的代码片段,可能涉及网络请求、数据持久化、UI优化等方面。开发者可以通过这些代码快速解决常见的问题,或者借鉴其中的设计思路。 在实际开发中...
总的来说,EdmondAppsAndroidUtils作为一个开源项目,旨在通过提供一系列封装好的工具和方法,帮助Android开发者更快捷、高效地完成开发工作,同时保持代码的整洁和可维护性。其丰富的功能和简洁的API设计,无疑为...