Android手机多媒体的应用
2016-01-12 09:12
435 查看
对《第一行代码——Android》的读书笔记
接下来创建一个Nootification对象,这个对象用于存储通知所需的各种信息。
其构造函数接受三个参数:1.参数用于指定通知的图标
2.指定通知的ticker类型
3.指定通知被创建的时间
然后需要对通知的布局进行设定,这了只需调用Notification的setLatestEventInfo()方法就可以给通知设置一个标准的布局。
这个方法接受四个参数:
1.Context
2.制定标题内容
3.指定通知的内容
4.暂时传入NULL
Notification.setLatestEventInfo(context,"this is content title","this is content text",null);
都完成之后,只需调用NotificationManager的notify()方法就可以让通知显示出来。
该方法接受两个参数:
1.ID 保证每个通知所指定的Id都是不同的。
2.Notification对象
manager.notify(1,notification);
这几个方法传入的参数都是相通的:
1.Context
2.通常传入0即可
3.Intent对象 通过这个对象创建出意图
4.用于确定PendingIntent的行为。
Notification.setLatestEventInfo()的第四个参数传入的就是PendingIntent.
点击按钮后,会跳转另一个activity,并关闭通知
首先从intent参数中取出一个Bundle对象,然后使用pdu密钥来提取SMS pdus数组,其中每一个pdu都表示一条短消息。接着使用SmsMessage的createfromPdu()方法将每一个pdu字节数组转化为SmsMessage对象,调用这个对象的getOriginatingAddress()方法就可以获取到短信的发送方号码,调用getMessageBody()方法就可以获取短信的内容。最后将方法和内容显示在textview中。
完成之后,需要对它进行注册才能让他接收到短信广播。使用动态广播注册,在onCreate()注册,在onDesstroy()进行注销。当然还要添加权限。
发送短信与接收短信类似,发送时调用SmsManager()的getDefault()方法获取到SmsManager的实例,然后调用它的sendTextMessage()方法就可以去发送短线了。
sendTextMessage()方法接受五个参数,其中第一个参数用于指定接收人的手机号码,第三个参数用于指定短信的内容,其他暂时传入NULL。
PendingIntent的getBroadcast()方法获取一个PendingIntent对象,并将它作为第四个参数传递到sendTextMessage()方法中。然后注册一个新的广播接收器SendStatusReceiver,其专门用于监听短信发送状态,当getResultCode()的值等于RESULT_OK就会提示发送成功,否则提示发送失败。
播放音频
使用通知
通知是Android系统中比较特色的一个功能。发出一条通知后,手机最上方的状态栏会显示一个通知的图标,下拉状态栏后就可以看到通知的详细内容。通知的基本用法
首先需要一个NotificationManger来对通知进行管理,可以调用Context的getSystemService()方法获取到。getSystemService()方法接受一个字符串参数用于确定获取系统的那个服务,这里我们传入Context.NOTIFICATION_SERVICE即可。NotificationManger manger=(NotificationManger)getSystemService(Context.NOTIFICATION_SERVICE);
接下来创建一个Nootification对象,这个对象用于存储通知所需的各种信息。
其构造函数接受三个参数:1.参数用于指定通知的图标
2.指定通知的ticker类型
3.指定通知被创建的时间
Notification notification =new Notification(R.drawable.icon,"this is ticker text",System.currentTimeMillis());
然后需要对通知的布局进行设定,这了只需调用Notification的setLatestEventInfo()方法就可以给通知设置一个标准的布局。
这个方法接受四个参数:
1.Context
2.制定标题内容
3.指定通知的内容
4.暂时传入NULL
Notification.setLatestEventInfo(context,"this is content title","this is content text",null);
都完成之后,只需调用NotificationManager的notify()方法就可以让通知显示出来。
该方法接受两个参数:
1.ID 保证每个通知所指定的Id都是不同的。
2.Notification对象
manager.notify(1,notification);
通知点击响应——PendingIntent
通过getActivity(),getBroadcast(),getService这几个静态方法都可以获得其对象。这几个方法传入的参数都是相通的:
1.Context
2.通常传入0即可
3.Intent对象 通过这个对象创建出意图
4.用于确定PendingIntent的行为。
Notification.setLatestEventInfo()的第四个参数传入的就是PendingIntent.
public class MainActivity extends Activity implements OnClickListener { private Button sendNotice; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendNotice = (Button) findViewById(R.id.send_notice); sendNotice.setOnClickListener(this); } @Override public void onClick(View v) { 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()); Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg")); notification.sound = soundUri; long[] vibrates = {0, 1000, 1000, 1000}; notification.vibrate = vibrates; notification.ledARGB = Color.GREEN; notification.ledOnMS = 1000; notification.ledOffMS = 1000; notification.flags = Notification.FLAG_SHOW_LIGHTS; // notification.defaults = Notification.DEFAULT_ALL; 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); break; default: break; } } }
点击按钮后,会跳转另一个activity,并关闭通知
public class NotificationActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification_layout); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.cancel(1); } }
接收和发送短信
接收短信
当手机接收到一条短信的时候,系统会发出一条值为android.provider.Telephony.SMS_RECEIVED的广播,这条广播里携带的与短信相关的数据。每个应用程序都可以在广播接收器里对它进行监听,收到广播解析出短信的内容即可。class MessageReceiver extends BroadcastReceiver { @Override 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 < messages.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } String address = messages[0].getOriginatingAddress(); String fullMessage = ""; for (SmsMessage message : messages) { fullMessage += message.getMessageBody(); } sender.setText(address); content.setText(fullMessage); abortBroadcast(); } }
首先从intent参数中取出一个Bundle对象,然后使用pdu密钥来提取SMS pdus数组,其中每一个pdu都表示一条短消息。接着使用SmsMessage的createfromPdu()方法将每一个pdu字节数组转化为SmsMessage对象,调用这个对象的getOriginatingAddress()方法就可以获取到短信的发送方号码,调用getMessageBody()方法就可以获取短信的内容。最后将方法和内容显示在textview中。
完成之后,需要对它进行注册才能让他接收到短信广播。使用动态广播注册,在onCreate()注册,在onDesstroy()进行注销。当然还要添加权限。
发送短信与接收短信类似,发送时调用SmsManager()的getDefault()方法获取到SmsManager的实例,然后调用它的sendTextMessage()方法就可以去发送短线了。
sendTextMessage()方法接受五个参数,其中第一个参数用于指定接收人的手机号码,第三个参数用于指定短信的内容,其他暂时传入NULL。
PendingIntent的getBroadcast()方法获取一个PendingIntent对象,并将它作为第四个参数传递到sendTextMessage()方法中。然后注册一个新的广播接收器SendStatusReceiver,其专门用于监听短信发送状态,当getResultCode()的值等于RESULT_OK就会提示发送成功,否则提示发送失败。
public class MainActivity extends Activity {
private TextView sender;
private TextView content;
private EditText to;
private EditText msgInput;
private Button send;
private IntentFilter receiveFilter;
private MessageReceiver messageReceiver;
private IntentFilter sendFilter;
private SendStatusReceiver sendStatusReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sender = (TextView) findViewById(R.id.sender);
content = (TextView) findViewById(R.id.content);
to = (EditText) findViewById(R.id.to);
msgInput = (EditText) findViewById(R.id.msg_input);
send = (Button) findViewById(R.id.send);
receiveFilter = new IntentFilter();
receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
receiveFilter.setPriority(100);
messageReceiver = new MessageReceiver();
registerReceiver(messageReceiver, receiveFilter);
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);
smsManager.sendTextMessage(to.getText().toString(), null,
msgInput.getText().toString(), pi, null);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(messageReceiver);
unregisterReceiver(sendStatusReceiver);
}
class MessageReceiver extends BroadcastReceiver { @Override 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 < messages.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } String address = messages[0].getOriginatingAddress(); String fullMessage = ""; for (SmsMessage message : messages) { fullMessage += message.getMessageBody(); } sender.setText(address); content.setText(fullMessage); abortBroadcast(); } }
class SendStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (getResultCode() == RESULT_OK) {
Toast.makeText(context, "Send succeeded", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(context, "Send failed", Toast.LENGTH_LONG)
.show();
}
}
}
}
调用摄像头和相册
调用摄像头拍照
public class MainActivity extends Activity { public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 2; private Button takePhoto; private Button chooseFromAlbum; private ImageView picture; private Uri imageUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); takePhoto = (Button) findViewById(R.id.take_photo); chooseFromAlbum = (Button) findViewById(R.id.choose_from_album); picture = (ImageView) findViewById(R.id.picture); 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"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 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); } }); } @Override 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.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; } } }
播放多媒体文件
播放音频public class MainActivity extends Activity implements OnClickListener {
private Button play;
private Button pause;
private Button stop;
private MediaPlayer mediaPlayer = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
initMediaPlayer();
}
private void initMediaPlayer() {
try {
File file = new File(Environment.getExternalStorageDirectory(), "music.mp3");
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
break;
case R.id.pause:
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
break;
case R.id.stop:
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
initMediaPlayer();
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
播放视频文件public class MainActivity extends Activity implements OnClickListener {
private VideoView videoView;
private Button play;
private Button pause;
private Button replay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
replay = (Button) findViewById(R.id.replay);
videoView = (VideoView) findViewById(R.id.video_view);
play.setOnClickListener(this);
pause.setOnClickListener(this);
replay.setOnClickListener(this);
initVideoPath();
}
private void initVideoPath() {
File file = new File(Environment.getExternalStorageDirectory(), "movie.3gp");
videoView.setVideoPath(file.getPath());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if (!videoView.isPlaying()) {
videoView.start();
}
break;
case R.id.pause:
if (videoView.isPlaying()) {
videoView.pause();
}
break;
case R.id.replay:
if (videoView.isPlaying()) {
videoView.resume();
}
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (videoView != null) {
videoView.suspend();
}
}
}
相关文章推荐
- 《Android源码设计模式解析与实战》读书笔记(十九)
- 最优雅退出 Android 应用程序的 6 种方式
- 推荐提升 Android 性能的建议
- Android 自己定义ScrollView ListView 体验各种纵向滑动的需求
- Android开发之浏览器用法实例详解(调用uc,opera,qq浏览器访问网页)
- 推荐提升 Android 性能的建议
- android WindowManager解析与骗取QQ密码案例分析
- 推荐提升 Android 性能的建议
- Android之Animation-list实现逐帧动画
- android surfaceview播放视频前,出现黑屏
- android 选择图片 剪裁 拍照 兼容所有版本的代码
- Android 进程生命周期 Process Lifecycle
- Android中 Window 、WindowManager、WindowManager.LayoutParams 相关内容
- 写一个Android输入法01——最简步骤
- Android实习札记(10)---ImageView的src属性 VS blackground
- Android实习札记(9)---设置Eclpise为护眼色与代码快速对齐
- Android实习札记(8)---ViewPager+Fragment实例详解
- Android实习札记(6)---ViewPager使用详解
- Android实习札记(5)---Fragment之底部导航栏的实现
- Android实习札记(4)---Fragment(碎片)基本概念解析