当前位置:操作系统 > 安卓/Android >>

android开发中较有用的代码片段

1、显示toast

[java]
Toast.makeText(this._getApplicationContext(), R.string._item, Toast.LENGTH_SHORT).show(); 
Toast.makeText(this._getApplicationContext(), R.string._item, Toast.LENGTH_SHORT).show();[java]
2、用log进行查看 
2、用log进行查看[java]
private static final TAG="MainActivity"; 
Log.i(TAG,e.toString()); 
private static final TAG="MainActivity";
Log.i(TAG,e.toString());[java]
3、设置进度条 
3、设置进度条[java]
mProgressDialog = new ProgressDialog(this); 
mProgressDialog.setTitle("提示"); // 设置标题  
mProgressDialog.setMessage("正在提交汇报内容,请稍候..."); // 设置body信息  
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // 设置进度条样式是  
mProgressDialog.setCancelable(false); 
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setTitle("提示"); // 设置标题
mProgressDialog.setMessage("正在提交汇报内容,请稍候..."); // 设置body信息
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // 设置进度条样式是
mProgressDialog.setCancelable(false);4、拍摄照片
[java]
Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
strPath = Environment.getExternalStorageDirectory().toString()+ "/CONSDCGMPIC/";// 存放照片的文件夹  
fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ ".jpg";// 照片命名  
File out = new File(strPath); 
if (!out.exists()) { 
out.mkdirs(); 

out = new File(strPath, fileName); 
strPath = strPath + fileName;// 该照片的绝对路径  
imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 
startActivityForResult(imageCaptureIntent, RESULT_CAPTURE_IMAGE); 

Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
strPath = Environment.getExternalStorageDirectory().toString()+ "/CONSDCGMPIC/";// 存放照片的文件夹
fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ ".jpg";// 照片命名
File out = new File(strPath);
if (!out.exists()) {
out.mkdirs();
}
out = new File(strPath, fileName);
strPath = strPath + fileName;// 该照片的绝对路径
imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(imageCaptureIntent, RESULT_CAPTURE_IMAGE);
}[java]
5、拍摄视频 
5、拍摄视频[java]
<pre class="java" name="code">Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);  
<pre class="java" name="code">Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); [java]
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);  
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); [java]
startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO); 
startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);[java]
6、录音 
6、录音[java]
<pre class="java" name="code">Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setType("audio/amr"); 
startActivityForResult(intent, RESULT_CAPTURE_RECORDER_SOUND); 
<pre class="java" name="code">Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/amr");
startActivityForResult(intent, RESULT_CAPTURE_RECORDER_SOUND);

 

7、查看是否有存储卡插入

[java]
String status=Environment.getExternalStorageState(); 
 
if(status.equals(Enviroment.MEDIA_MOUNTED)) 
 

 

String status=Environment.getExternalStorageState();

if(status.equals(Enviroment.MEDIA_MOUNTED))

{

}
 

8、让某个Activity透明

        OnCreate中不设Layout

[java]
this.setTheme(R.style.Theme_Transparent); 
this.setTheme(R.style.Theme_Transparent);
以上是Theme_Transparent的定义(注意transparent_bg是一副透明的图片)

9、在屏幕元素中设置句柄
使用Activity.findViewById来取得屏幕上的元素的句柄. 使用该句柄您可以设置或获取任何该对象外露的值.

[java]
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);


10、发送短信
[java]
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);


11、发送彩信
[java]
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);


12、发送Mail
[java]
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);


13、注册一个BroadcastReceiver
[java]
registerReceiver(mMa

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,