第八章 多媒体

来源:互联网 发布:直播视频录制软件 编辑:程序博客网 时间:2024/05/17 01:02

第八章 多媒体
相比于广播接收器和服务,在活动里创建通知的场景还是比较少的, 因为一般只有当程序进入到后台的时候我们才需要使用通知。
NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(R.drawable.icon, “This is ticker text”, System.currentTimeMillis());
notification.setLatestEventInfo(context, “This is content title”, “This is content text”, null);
manager.notify(1, notification);

Intent 更加倾向于去立即执行某个动作,而 PendingIntent 更加倾向于在某个合适 的时机去执行某个动作

int FLAG_CANCEL_CURRENT:如果该PendingIntent已经存在,则在生成新的之前取消当前的。
int FLAG_NO_CREATE:如果该PendingIntent不存在,直接返回null而不是创建一个PendingIntent.
int FLAG_ONE_SHOT:该PendingIntent只能用一次,在send()方法执行后,自动取消。
int FLAG_UPDATE_CURRENT:如果该PendingIntent已经存在,则用新传入的Intent更新当前的数据。
我们需要把最后一个参数改为PendingIntent.FLAG_UPDATE_CURRENT,这样在启动的Activity里就可以用接收Intent传送数据的方法正常接收。

//发送广播
switch (v.getId()) {
case R.id.send_notice:
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.
ic_launcher, “This is ticker text”, System.currentTimeMillis());
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);//说明一点击就跳入一个activity。
notification.setLatestEventInfo(this, “This is content title”, “This is content text”, pi);
manager.notify(1, notification);

//在新的咦?怎么系统状态上的通知图标还没有消失呢?是这样的,如果我们没有在代码中对该 通知进行取消,它就会一直显示在系统的状态栏上显示。解决的方法也很简单,调用 NotificationManager 的 cancel()方法就可以取消通知了。修改 NotificationActivity 中的代
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);
long[] vibrates={0,1000,1000,1000};
notification.vibrate=vibrates;

notification.defaults=Notification.DEFAULT_ALL;

当手机接收到一条短信的 时候,系统会发出一条值为 android.provider.Telephony.SMS_RECEIVED 的广播,这条广播里 携带着与短信相关的所有数据。每个应用程序都可以在广播接收器里对它进行监听,收到广 播时再从中解析出短信的内容即可。
android.privider.Telephony.SMS_RECEIVED.

bundle bundle=intent.getExtras();
Object[] pdus=(Object[])bundle.get(“pdus”);//获取短信小心 二进制数组
SmsMessage[] message=new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
String address = messages[0].getOriginatingAddress(); // 获取发
String fullMessage = “”;
for (SmsMessage message : messages) {
fullMessage += message.getMessageBody(); // 获取短信内容
323
第一行代码——Android
 }
sender.setText(address);
content.setText(fullMessage);
}

拦截广播 注册的时候 receiveFilter.setPriority(100);
在BroadcastReceiver 的onReceive方法中abortBroadcast();

发送短信的功能 得到短信管理者,发送短信
SmsManager smsManager=Smsmanger.getDefault();
smsManager.sendTextMessage(to.getText().toString(),null,msgInput.getText().toString(),null,null);

sendFilter = new IntentFilter();
sendFilter.addAction(“SENT_SMS_ACTION”);
sendStatusReceiver = new SendStatusReceiver();
registerReceiver(sendStatusReceiver, sendFilter);

send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SmsManager smsManager = SmsManager.getDefault();
Intent sentIntent = new Intent(“SENT_SMS_ACTION”);
PendingIntent pi = PendingIntent.getBroadcast
(MainActivity.this, 0, sentIntent, 0);
//PendingIntent.getBroadcast(MainActivity.this,0,sentIntent,0); 意思是只要点击的话就会发送一条intent,执行onreceive方法。
smsManager.sendTextMessage(to.getText().toString(), null,
msgInput.getText().toString(), pi, null);
}

每条短信的长度不得超过 160 个字符,如果想要发送超出这个长 度的短信,则需要将这条短信分割成多条短信来发送,使用 SmsManager 的 sendMultipart- TextMessage()方法就可以实现上述功能。

对于显示Intent,Android不需要再去做解析,因为目标组件很明确。Android需要解析的是隐式Intent,通过解析,将Intent映射给可以处理该Intent的Activity,Service等。Intent的解析机制主要是通过查找已经注册在AndroidManifest.xml中的所有IntentFilter以及其中定义的Intent,最终找到匹配的Intent。

Intent寻找目标组件的两种方式:
显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的。
隐式Intent:通过Intent Filter来实现的,它一般用在没有明确指出目标组件名称的前提下,一般是用于在不同应用程序之间。
一.显式Intent

摄像头

takePhoto.setOnClickListener(new OnClickListener() {
@Override
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();
}
imageUri = Uri.fromFile(outputImage);
//打开照相机
Intent intent = new Intent(“android.media.action.IMAGE_CAPTURE”);
//添加额外信息
// 创建File对象,用于存储拍照后的图片
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//打开activity。
startActivityForResult(intent, TAKE_PHOTO); //打开照相机
}
});
chooseFromAlbum.setOnClickListener(new OnClickListener() {
@Override
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();
}
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, imageUri);
startActivityForResult(intent, CROP_PHOTO); //打开裁减
}
});
}

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    switch (requestCode) {    //如果请求码是TAKE_PHOTO 照完相之后裁减 之后显示    case TAKE_PHOTO:        if (resultCode == RESULT_OK) {            Intent intent = new Intent("com.android.camera.action.CROP");            intent.setDataAndType(imageUri, "image/*");            intent.putExtra("scale", true);            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);            startActivityForResult(intent, CROP_PHOTO);               }        break;    case CROP_PHOTO:        //        if (resultCode == RESULT_OK) {            try {                Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()                        .openInputStream(imageUri));                picture.setImageBitmap(bitmap);            } catch (FileNotFoundException e) {                e.printStackTrace();            }        }        break;    default:        break;    }}

}

播放音频 private MediaPlayer mediaPlayer = new MediaPlayer();
播放视频

0 0