Android实用的代码片段 常用代码总结
作者:mdxy-dxy 发布时间:2022-02-02 20:29:53
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的消息机制主要是指Handler的运行机制,那么什么是Handler的运行机制那?通俗的来讲就是,使用Handler将
- .NET 中的正则表达式是基于 Perl 5 的正则表达式。超时从 .NET Framework 4.5 开始,正则表达式支持在匹配操作中指
- 一、deleteById 和 delete为什么要把这两个方法放在一起呢?我们先看源码再说deleteById(Id id)(通过id进行删
- 该着色方法一句着色图层中要素类的某个数值字段的属性值,按这个属性值为每种不同值得要素单独分配一种显示符号样式。关键在于获取该字段所有要素的唯
- 1.JMM数据原子操作read(读取)∶从主内存读取数据load(载入):将主内存读取到的数据写入工作内存use(使用):从工作内存读取数据
- 一、写在前面数据结构中的队列应该是比较熟悉的了,就是先进先出,因为有序故得名队列,就如同排队嘛,在对尾插入新的节点,在对首删除节点.jdk集
- 修改加密和验证方法/** * 生成BCryptPasswordEncoder密码 *
- SpringBoot使用过滤器、 * 和 * 一、SpringBoot使用过滤器Spring boot过滤器的使用(两种方式)使用sprin
- Spring的 * 缓存Spring * 缓存是为了解决对象间的循环依赖问题。A依赖B,B依赖A,这就是一个简单的循环依赖。我们来先看看 * 缓存
- 最近在折腾一些控制相关的软件设计,想起来状态机这个东西,对解决一些控制系统状态切换还是挺有用的。状态机(有限状态自动机)网上有很多介绍。简单
- 一、TCP/IP简介TCP/IP协议族是互联网使用的协议,也可以用在独立的专用网络中。TCP/IP协议族包括了IP协议、TCP协议和UDP协
- 1、使用步骤1)构建请求网址A、确定端点:每个GIS服务都有一个端点。例如,ArcGIS Server上Demographics文件夹下名为
- AsnyncLocal与ThreadLocal都是存储线程上下文的变量,但是,在实际使用过程中两者又有区别主要的表现在:AsyncLocal
- 健康检查是Spring Boot Actuator中重要端点之一,可以非常容易查看应用运行至状态。本文在前文的基础上介绍如何自定义健康检查。
- 使用DOM4J方式生成XML文件的步骤如下:引入JAR包通过DocumentHelper类的createDocument()创建Docume
- 经测试,是环绕通知改变了返回值,切面方法需要有返回值,来代替被代理方法返回结果改成如下即可:@Around("point_upda
- 前言在 C# 编程中,管道式编程(Pipeline Style programming)其实存在已久,最明显的就是我们经常使用的 LINQ。
- 使用ehcache-spring-annotations使得在工程中简单配置即可使用缓存下载地址:http://code.google.co
- springboot集成 redispom文件<dependency> <groupId>
- Android MotionEvent中getX()和getRawX()的区别实例详解实例代码:public class Res exten