Android 开发者实用代码片段
AndroidJavaXMLUP
Android 很有用的代码片段 1:查看是否有存储卡插入 :
Java 代码
1 2 3 4 5 6 } String status=Environment.getExternalStorageState(); if(status.equals(Enviroment.MEDIA_MOUNTED)) { 说明有 SD 卡插入
Java 代码
7 8 9 10
String status=Environment.getExternalStorageState(); if(status.equals(Enviroment.MEDIA_MOUNTED)) { 说明有 SD 卡插入
11 }
2:让某个 Activity 透明 OnCreate 中不设 Layout this.setTheme(R.style.Theme_Transparent); 以下是 Theme_Transparent 的定义(注意 transparent_bg 是一副透明的图片)
3:在屏幕元素中设置句柄 使用 Activity.findViewById 来取得屏幕上的元素的句柄. 使用该句柄您可以设置或获取任何该对象外露的 值.
Java 代码
12 TextView msgTextView = (TextView)findViewById(R.id.msg); 13
Java 代码
msgTextView.setText(R.string.push_me);
14 TextView msgTextView = (TextView)findViewById(R.id.msg); 15 msgTextView.setText(R.string.push_me);
4:发送短信
Java 代码
16
String body=”this is mms demo”;
17 Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”smsto”, number, null)); 18 mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body); 19 mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true); 20 mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true); 21
Java 代码
startActivity(mmsintent);
22
String body=”this is mms demo”;
23 Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”smsto”, number, null));
24 mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body); 25 mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true); 26 mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true); 27 startActivity(mmsintent);
5:发送彩信
Java 代码
28 StringBuilder sb = new StringBuilder(); 29 30 31 sb.append(”file://”); sb.append(fd.getAbsoluteFile()); Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”mmsto”,
number, null)); 32 33 // Below extra datas are all optional. intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT,
subject); 34 35 intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body); intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI,
sb.toString()); 36 intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE,
composeMode); 37 intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT,
exitOnSent); 38
Java 代码
startActivity(intent);
39 StringBuilder sb = new StringBuilder(); 40 41 42 sb.append(”file://”); sb.append(fd.getAbsoluteFile()); Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”mmsto”,
number, null)); 43 44 // Below extra datas are all optional. intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT,
subject); 45 46 intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body); intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI,
sb.toString()); 47 intent.put
Extra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE,
composeMode); 48 intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT,
exitOnSent); 49 startActivity(intent);
6:发送 Mail
Java 代码
50
mime = “img/jpg”;
51 shareIntent.setDataAndType(Uri.fromFile(fd), mime); 52 shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.from