一、通知
需要NotificationManager對通知進行管理,getSystemService獲取系統中的那個服務。
NotificationManager manager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
用support-v4庫提供的NotificationCompat類來創建Notification對象,確保了兼容性。
創建一個豐富的Notification對象,setContentTitle設置標題,setContentText設置內容,setWhen用于指定通知被創建的時間。使用Build方法進行生成。
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
使用notify方法將通知顯示出來,notify()有兩個參數,一個是id,一個是剛才創建的notification對象。
manager.notify(1, notification);
現在的notify可以顯示出來,但是不能相應點擊事件。我們現在讓他點擊后進入NotificationActivity活動中。
先構建Intent對象,pendingIntent式延遲執行的Intent,根須需求選擇是使用getActivity還是getService。
Intent intent = new Intent(this,NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
第一個參數是context,第二和第四個參數通常傳入0就可以了。
在NotificationCompat.Builder中調用setContentIntent(pi)傳入即可。通知圖標在產生點擊事件時不會消失,要使其消失,有兩種方法:
- 要在NotificationActivity中添加
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);
- 或者NotificationCompat.Builder調用setAutoCancel(true)
通知的高級用法
允許播放音頻。
Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/ Basic_tone.ogg"));
notification.sound = soundUri;
震動。
//下標為0的值表示手機靜止的時長,下標為1的值表示手機振動的時長,下標為2的值又表示手機靜止的時長,以此類推。
setVibrate(new long[]{0,1000,1000,1000}).build();
notification.vibrate = vibrates;
LED燈
// LED 燈以綠色的燈光一閃一閃的效果
notification.ledARGB = Color.GREEN;
notification.ledOnMS = 1000;
notification.ledOffMS = 1000;
notification.flags =
Notification.FLAG_SHOW_LIGHTS;
二、短信
接收短信
定義一個接受者。
class MessageReceiver extends BroadcastReceiver { @Overridepublic 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);}
}
我們從 Intent 參數中取出了一個 Bundle 對象,然后使用 pdu 密鑰來提取一個 SMS pdus 數組, 其中每一個 pdu 都表示一條短信消息。接著使用 SmsMessage 的
createFromPdu() 方法將每一個 pdu 字節數組轉換為 SmsMessage 對象, 調用這個對象的
getOriginatingAddress()方法就可以獲取到短信的發送方號碼,調用 getMessageBody()方法就可以獲取到短信的內容,然后將每一個 SmsMessage 對象中的短信內容拼接起來,就組成了一條完整的短信。
在活動中的oncreate方法中綁定 事件 與 內容接受者。
receiveFilter = new IntentFilter();
receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
messageReceiver = new MessageReceiver();
registerReceiver(messageReceiver, receiveFilter);
接收短信
得到SmsManager實例,直接調用sendextMessage方法。
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(to.getText().toString(),
null,msgInput.getText().toString(), null, null);
sendTextMessage()方法接收五個參數,其中第一個參數用于指定接收人的手 機號碼,第三個參數用于指定短信的內容
類似通知,想要在發送短信之后知道短信的狀態,怎么辦呢?
sendFilter = new IntentFilter();
sendFilter.addAction("SENT_SMS_ACTION");
sendStatusReceiver = new SendStatusReceiver();
registerReceiver(sendStatusReceiver, sendFilter); send.setOnClickListener(new OnClickListener() {@Overridepublic 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);}
});class SendStatusReceiver extends BroadcastReceiver {@Overridepublic 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();}}
}
- 獲取到了一個 PendingIntent 對象,并將它作為第四個參數傳遞到 sendTextMessage()方法中
- PendingIntent 發出 SENT_SMS_ACTION 這樣的一個事件
- 新建一個事件接受者SendStatusReceiver,綁定該接受者 和 SENT_SMS_ACTION 事件。
三、攝像頭和相冊
定義輸出圖片的路徑啟動相機程序的Intent啟動相機并拍照進入onActivityResult,啟動截圖的Intent截圖成功把圖片顯示出來
四、多媒體文件
音頻
方法名功能描述
| setDataSource() | 設置要播放的音頻文件的位置。 |
| prepare() | 在開始播放之前調用這個方法完成準備工作。 |
| start() | 開始或繼續播放音頻。 |
| pause() | 暫停播放音頻。 |
| reset() | 將 MediaPlayer 對象重置到剛剛創建的狀態。 |
| seekTo() | 從指定的位置開始播放音頻。 |
| stop() | 停止播放音頻。調用這個方法后的 MediaPlayer 對象無法再播放音頻。 |
| release() | 釋放掉與 MediaPlayer 對象相關的資源。 |
| isPlaying() | 判斷當前 MediaPlayer 是否正在播放音頻。 |
| getDuration() | 獲取載入的音頻文件的時長。 |
首先需要創建出一個MediaPlayer 對象,然后調用 setDataSource()方法來設置音頻文件的路徑,再調用 prepare()方法使MediaPlayer 進入到準備狀態,接下來調用start()方法就可以開始播放音頻,調用pause() 方法就會暫停播放,調用 reset()方法就會停止播放。
視頻
方法名功能描述
| setVideoPath() | 設置要播放的視頻文件的位置。 |
| start() | 開始或繼續播放視頻。 |
| pause() | 暫停播放視頻。 |
| resume() | 將視頻重頭開始播放。 |
| seekTo() | 從指定的位置開始播放視頻。 |
| isPlaying() | 判斷當前是否正在播放視頻。 |
| getDuration() | 獲取載入的視頻文件的時長。 |
總結
以上是生活随笔為你收集整理的安卓学习 之 多媒体技术(八)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。