最近看了一下Android,只看书不总结一下,总感觉东西并没有放在脑子里,人过留名,雁过留声,遂写下这篇总结。
书本看的是《Android编程兵书》,但这篇总结的大纲是按照《第一行代码》的章节目录撰写的。两本书的内容结构大致相同,Android入门的话只看其中一本就够了。
使用通知:即可在activity中创建,也可在广播接收器中创建
PendingIntent:延迟执行的Intent
代码示例
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "This is a ticker text", System.currentTimeMillis());
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, "This is content title", "This is content text", pi);
Manager.notify(1, notification);
// 第一个参数指定notification的id,在NotificationActivity中根据此ID取消通知
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);
通知的高级技巧:通过nofification的sound属性指定铃声;vibrate属性控制震动和静止的时长;ledARGB控制LED呼吸灯的颜色, ledOnMS指定灯亮起的时长,ledOffMS指定灯暗去的时长
示例:实现通知到来时LED灯以绿色的灯光一闪一闪
notification.ledARGB = Color.GREEN;
notification.ledOnMS = 1000;
notification.ledOffMS = 1000;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
//也可完全根据当前手机的环境来决定播放的铃声以及震动
notification.defaults = Notification.DEFAULT_ALL;
接收和发送短信
接收短信:主要利用广播机制(有序广播)
示例代码
receiverFilter.setPriority(100); // 提高优先级
Class MessageReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent){
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[])bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for(int i = 0; i < SmsMessage.length; i++){
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
String address = message[0].getOriginationgAddress();
String fullMessage = "";
for(SmsMessage message : messages){
fullMessage += message.getMessageBody();
}
sender.setText(address);
content.setText(fullMessage);
abortBroadcast(); // 中止广播继续传递
}
}
调用摄像头和相册
调用摄像头示例代码
public class MainActivity extends Activity{
public static final int TAKE_PHOTO = 1;
public static final int CROP_PHOTO = 2;
private Button takePhoto;
private ImageView picture;
private Uri iamgeUri;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePhoto = (Button)findViewById(R.id.takePhoto);
picture = (ImageView)findViewById(R.id.picture);
takePhoto.setOnclickListener(new OnClickListener(){
public void onClick(View v){
File outputImage = new File(Environment.getExternalStorageDirectory(), "output_image.jpg");
try{
if(outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
iamgeUri = Uri.fromFile(outputImage);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, iamgeUri);
startActivityForResult(intent, TAKE_PHOTO);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch(requestCode){
case TAKE_PHOTO:
if(resultCode == RESULT_OK){
Intent intent = new Intent("com.android.camera.cation.CROP");
intent.setDataAndType(iamgeUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, iamgeUri);
startActivityForResult(intent, CROP_PHOTO);
}
break;
}
case CROP_PHOTO:
if(resultCode == RESULT_OK){
try{
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(iamgeUri));
picture.setImageBitmap(bitmap);
}catch(FileNotFoundException e){
e.printStackTrace();
}
}
break;
default:
break;
}
}
从相册中选取照片
File outputImage = new File(Environment.getExtranalStorageDirectory(), "output_image.jpg");
try{
if(outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
intent.putExtra("crop", true);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, iamgeUri);
startActivityForResult(intent, CROP_PHOTO);
Tips
1、android 4.2 版本之后默认时把开发者选项隐藏掉的,进入关于手机界面,对版本号连击4次,即可开启开发者选项。
网友评论