Lập trình android C12 audio video camera telephone SMS

54 65 0
Lập trình android  C12 audio video camera telephone SMS

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Ch ng 12 AudioVideo - CameraTelephone - SMS Lê V n H nh levanhanhvn@gmail.com N I DUNG 1.Gi i thi u v Media Player 2.Xây d ng Media Player Audio & Video 3.Các u n Media Player 4.Recoding Audio & Video i u n ch p hình v i Camera 6.L u t p tin media vào Media Store 7.Telephone 8.SMS GI I THI U V MEDIAPLAYER • Android cung c p l p MediaPlayer đ qu n lý tác v đa truy n thông bao g m t p tin âm thanh, hình nh, video • Có th truy xu t t p tin media thông qua vi c l u tr nh tài nguyên ng d ng, b nh thi t b , content provider ho c thơng qua URL • MediaPlayer qu n lý t p tin media lu ng x lý thông qua m t t p tr ng thái sau: o Kh i t o đ i t ng MediaPlayer o Chu n b b thu phát MediaPlayer o B t đ u th c hi n thu phát o Th c hi n thao tác Pause Stop t p tin media thu phát o Hồn thành q trình thu phát XỂY D NG MEDIAPLAYER AUDIOAndroid h tr truy xu t t p tin Audio thông qua l u tr nh : tài nguyên ng d ng, b nh thi t b , content provider x lý lu ng URL • Có th đóng gói t p tin Audio vào th m c res/raw nh m t d ng tài nguyên c a ng d ng • thu phát m t t p tin Audio c n t o m t đ i t ng MediaPlayer thi t l p ngu n d li u cho đ i t ng • Th c hi n thu phát b ng ph ng th c Create() truy n vào tham s : context c a ng d ng m t nh ng d ng tài nguyên sau: o nh danh c a tài nguyên o URI tr đ n n i l u tr c a t p tin thi t b o URI tr đ n t p tin tr c n thông qua URL o URI tr đ n dòng d li u b ng c a content provider Xây d ng MediaPlayer Audio • th c thi vi c thu phát t p tin video c n t o m t hình cho vi c trình chi u Có hai cách đ th c hi n công vi c này: o S d ng thành ph n Video View, đóng gói thao tác đ i t ng MediaPlayer (t o trình chi u, chu n b c p phát t p tin) vào thành ph n View o Xây d ng hình hi n th riêng g n k t d li u thông qua đ i t ng MediaPlayer Xây d ng MediaPlayer Audio • Ví d t o đ i t ng MediaPlayer • Ví d t o b thu phát b ng Video View Xây d ng MediaPlayer Audio • Ví d t o hình hi n th Video View Xây d ng MediaPlayer Audio • T o đ i t ng Surface Holder đ h tr vi c c p nh t ngu n x lý bên d i Xây d ng MediaPlayer Audio • Kh i t o n i dung c a t p tin c n trình chi u CÁC I U KHI N TRểN MEDIAPLAYER • Kh i ch y đ i t Vd: ng b ng ph ng th c start() Mediaplayer mediaPlayer = new MediaPlayer(); mediaPlayer.start(); • MediaPlayer cung c p m t s ph ng th c cho vi c u n nh getDuration, getCurrentPositon, seekTo Vd: Telephone • Ví d đ ng kí Intent AndroidManifest Telephone • Ví d x lý cu c g i t i SMS • G i tin nh n b ng cách t o m t Intent đ g i ng d ng Message thi t b Vd: //T o tin nh n SMS Intent smsIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse(“sms:123456”)); smsIntent.putExtra(“sms_body”, “Press send to send me”); startActivity(smsIntent); • G i tin nh n MMS có ch a t p tin Media Vd: //T o tin nh n MMS Uri attached_Uri = Uri.parse("content://media/external/images/media/1"); Intent mmsIntent = new Intent(Intent.ACTION_SEND,attached_Uri); mmsIntent.putExtra("sms_body", "Please see the attached image"); mmsIntent.putExtra("address", "07912355432"); mmsIntent.putExtra(Intent.EXTRA_STREAM, attached_Uri); mmsIntent.setType("image/png"); startActivity(mmsIntent); SMS • G i tin nh n MMS có ch a t p tin Media Vd: //T o tin nh n MMS Uri attached_Uri = Uri.parse("content://media/external/images/media/1"); Intent mmsIntent = new Intent(Intent.ACTION_SEND,attached_Uri); mmsIntent.putExtra("sms_body", "Please see the attached image"); mmsIntent.putExtra("address", "07912355432"); mmsIntent.putExtra(Intent.EXTRA_STREAM, attached_Uri); mmsIntent.setType("image/png"); startActivity(mmsIntent); SMSSMS tin nh n đ c u n b i SmsManager o T ođ it ng SmsManager SmsManager smsManager = SmsManager.getDefault(); o Yêu c u quy n truy c p g i tin nh n • Vi c g i tin nh n đ c th c hi n b i ph ng th c sendTextMessage c a l p SmsManager Cú pháp: sendTextMessage( Vd: Stringdestination Address, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent) SMS • Theo dõi, xác nh n SMS tin nh n đ c g i b ng cách đ ng ký BroadcastReceiver đ l ng nghe ho t đ ng t Pending Itent đ c t o g i tin nh n o sentItent đ c g i lên tin nh n đ công hay l i c g i thành Activity.RESULT_OK SmsManager.RESULT_ERROR_GENERIC_FAILURE SmsManager.RESULT_ERROR_RADIO_OFF SmsManager.RESULT_ERROR_NULL_PDU o deliveryIntent đ đ c tin nh n c g i lên m t thi t b đích nh n SMS • Ví d v ki m sốt g i tin nh n: t o Pending Intent String SENT_SMS_ACTION = "SENT_SMS_ACTION " ; String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION " ; Intent sentIntent = new Intent(SENT_SMS_ACTION); PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(),0, sentIntent,0); Intent deliveryIntent = new Intent(DELIVERED_SMS_ACTION); PendingIntent deliverPI = PendingIntent.getBroadcast(getApplicationContext(),0, deliveryIntent,0); SMS • Ví d v ki m sốt g i tin nh n: t o Broadcast Receiver cho Send_Action registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { switch (getResultCode()) { case Activity.RESULT_OK : break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: break; case SmsManager.RESULT_ERROR_RADIO_OFF : break; case SmsManager.RESULT_ERROR_NULL_PDU : break; } } },new IntentFilter(SENT_SMS_ACTION)); SMS • Ví d v ki m soát g i tin nh n: t o Broadcast Receiver cho Delivery_Action registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { [ SMS delivered actions ] } }, new IntentFilter(DELIVERED_SMS_ACTION)); smsManager.sendTextMessage(sendTo, null, myMessage, sentPI, deliverPI); SMS • X lý tin nh n dài h n 160 ký t b ng ph ng th c divideMessage() sendMultipartTextMessage() o Cú pháp: sendMultipartTextMessage(Stringdestination Address, String scAddress, ArrayList parts, ArrayList sentIntents, ArrayList deliveryIntents) o Ví d : ArrayList messageArray = smsManager.divideMessage(myMessage); ArrayList sentIntents = new ArrayList(); for (int i = 0; i < messageArray.size(); i++) sentIntents.add(sentPI); smsManager.sendMultipartTextMessage(sendTo, null, messageArray sentIntents, null); SMS • G i tin nh n ch a d li u b ng m ng byte o Cú pháp: sendDataMessage(Stringdestination Address, String scAddress, short destination Port, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent) o Ví d : Intent sentIntent = new Intent(SENT_SMS_ACTION); PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, sentIntent, 0); short destinationPort = 80; byte[] data = [ your data ]; smsManager.sendDataMessage(sendTo, null, destinationPort, data, sentPI, null); SMS • ng d ng nh n đ c SMS, c n đ ng ký BroadcastReceiver v i action • Cung c p quy n truy c p nh n SMS tin nh n SMS • Ví d t o Broadcast Receiver đ nh n tin nh n SMS • Làm vi c v i th m c tin nh n, c n đ ng ký quy n AndroidManifest.xml • Th c hi n truy v n đ l y thông tin v tin nh n th m c v i URI t ng ng o All : content://sms/all o Inbox : content://sms/inbox o Sent : content://sms/sent o Draft : content://sms/draft o Outbox : content://sms/outbox o Failed : content://sms/failed o Queued : content://sms/queued o Undelivered : content://sms/undelivered o Conversations : content://sms/conversations SMS • Ví d truy v n l y n i dung tin nh n Inbox

Ngày đăng: 21/12/2017, 18:59

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan