第八章 丰富你的程序,运用手机多媒体

2016-02-18  本文已影响0人  wyxjoker

8.1使用通知

当应用程序不在前台是,通过通知向用户发送提示消息.发送后,最上方的状态栏会显示通知的图标.,下拉后可以看到详细内容.

8.1.1通知的基本用法

通知可以由活动/服务/广播接收器创建.

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);

示例(别信书上的...书上太老了):

public void onClick(View v){
    switch (v.getId()){
        case R.id.send_notice:
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            Notification notification = new Notification.Builder(this)                    .setAutoCancel(true)
                    .setContentTitle("title")
                    .setContentText("describe")                   
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setWhen(System.currentTimeMillis())
                    .build();
            manager.notify(1,notification);
            break;
        default:
            break;
    }
}
Intent intent = new Intent(this,NotificationActifity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
        .setAutoCancel(true)
        .setContentTitle("title")
        .setContentText("describe")
        .setContentIntent(pi)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setWhen(System.currentTimeMillis())
        .build();
manager.notify(1,notification);

8.1.2通知的高级技巧####

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.defaults = Notification.DEFAULT_ALL;

8.2接受和发送短信###

8.2.1接收短信

class MessageReceive 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);
    }
}

注意:

Android设备接收到的SMS是以pdu形式的(protocol description unit)。所以从intent提取数据时就会遇到pdus。

8.2.2发送短信

有序广播是可以被截断的,而系统发出的短信广播是一条有序广播.先提高MessageReceive的优先级,再在onReceive()中调用abortBroadcast()中止即可.

示例:

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

msgInput = (EditText) findViewById(R.id.msg_input);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.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);
    }
});

8.3调用摄像头和相册

8.3.1将程序运行到手机上

略...

8.3.1从相册中选择照片

略...
相信我,如果你习惯筛选自己的名字来输出日志,千万千万记得把异常也用一样的方式输出 :)

8.4播放多媒体文件

8.4.1播放音频

Android中播放音频一般使用MediaPlayer实现.
常用方法:

注意:

读取SD卡文件需要获取权限.

8.4.2播放视频

Android中播放视频一般使用VideoView实现.

上一篇 下一篇

猜你喜欢

热点阅读