• $《第一行代码:Android》读书笔记——第8章 通知和手机多媒体


      本章主要介绍了通知、短信、调用摄像头和相册、播放多媒体文件等内容。  

    (一)通知的用法

      1、通知的基本用法

      见如下代码(详细操作步骤在代码注释中):

      (1)先创建一个布局文件,其中只有一个名为“发送通知”的Button,当点击这个按钮的时候发送一条通知:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical" >
     5 
     6     <Button
     7         android:id="@+id/sent_notice_btn"
     8         android:layout_width="match_parent"
     9         android:layout_height="wrap_content"
    10         android:text="发送通知" />
    11 
    12 </LinearLayout>

      (2)MainActivity:

     1 import android.app.Activity;
     2 import android.app.Notification;
     3 import android.app.NotificationManager;
     4 import android.os.Bundle;
     5 import android.view.Menu;
     6 import android.view.MenuItem;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.widget.Button;
    10 
    11 public class MainActivity extends Activity implements OnClickListener {
    12 
    13     private Button sendNoticeBtn;
    14 
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.activity_main);
    19 
    20         Button sendNoticeBtn = (Button) findViewById(R.id.sent_notice_btn);
    21         sendNoticeBtn.setOnClickListener(this);
    22     }
    23 
    24     @Override
    25     public void onClick(View v) {
    26         switch (v.getId()) {
    27         case R.id.sent_notice_btn:
    28             // 1.通过getSystemService方法创建NotificationManager实例
    29             NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    30             
    31             // 2.创建Notification实例,它是在屏幕上方一闪而过的那种通知信息
    32             // 第一个参数表示图标,第二个参数表示通知内容,第三个参数用于指定通知被创建的时间,当下拉状态时,这个时间会显示在对应的通知上
    33             Notification notification = new Notification(
    34                     R.drawable.ic_launcher, "您有一条通知",
    35                     System.currentTimeMillis());
    36             
    37             // 3.对下拉状态栏后显示的通知的布局进行设定
    38             // 第一个参数表示context上下文,第二个参数表示通知的标题,第三个参数表示通知的内容,第四个参数表示点击通知后的行为,这里先传入null
    39             notification.setLatestEventInfo(this, "这是通知标题", "这是通知内容", null);
    40             
    41             // 4.使用NotificationManager的notify方法让通知显示出来
    42             // 第一个参数:通知的id,具有唯一性;第二个参数:Notification对象
    43             mgr.notify(1, notification);
    44             break;
    45 
    46         default:
    47             break;
    48         }
    49     }
    50 }

      2、为通知加上点击跳转功能(使用PendingIntent)

      (1)在以上代码的基础上,再创建另一个xml文件,作为点击通知后跳转到的页面:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <TextView
     8         android:layout_width="wrap_content"
     9         android:layout_height="wrap_content"
    10         android:text="这是通知跳转的页面" />
    11 
    12 </LinearLayout>

      (2)创建活动NotificationActivity,在其中调用NotificationManager的cancel方法让状态栏的通知消失:

     1 import android.app.Activity;
     2 import android.app.NotificationManager;
     3 import android.os.Bundle;
     4 
     5 public class NotificationActivity extends Activity {
     6     @Override
     7     protected void onCreate(Bundle savedInstanceState) {
     8         super.onCreate(savedInstanceState);
     9         setContentView(R.layout.activity_notification);
    10 
    11         // 用于让状态栏上的通知消失,这里的1就是在MainActivity中创建通知时为通知设置的id
    12         NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    13         mgr.cancel(1);
    14     }
    15 }

      (3)修改MainActivity活动的按钮点击事件:

     1   @Override
     2     public void onClick(View v) {
     3         switch (v.getId()) {
     4         case R.id.sent_notice_btn:
     5             // 1.通过getSystemService方法创建NotificationManager实例
     6             NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
     7 
     8             // 2.创建Notification实例,它是在屏幕上方一闪而过的那种通知信息
     9             // 第一个参数表示图标,第二个参数表示通知内容,第三个参数用于指定通知被创建的时间,当下拉状态时,这个时间会显示在对应的通知上
    10             Notification notification = new Notification(
    11                     R.drawable.ic_launcher, "您有一条通知",
    12                     System.currentTimeMillis());
    13 
    14             // 3.对下拉状态栏后显示的通知的布局和点击行为进行设定
    15             // 第一个参数表示context上下文,第二个参数表示通知的标题,第三个参数表示通知的内容,第四个参数表示点击通知后的行为,这里先传入null
    16 
    17             // 3.1 创建Intent,用于在点击通知时跳转到NotificationActivity页面:
    18             Intent intent = new Intent(MainActivity.this,
    19                     NotificationActivity.class);
    20             // 3.2 创建PendingIntent
    21             PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0,
    22                     intent, PendingIntent.FLAG_CANCEL_CURRENT);
    23 
    24             notification.setLatestEventInfo(this, "这是通知标题", "这是通知内容", pi);
    25 
    26             // 4.使用NotificationManager的notify方法让通知显示出来
    27             // 第一个参数:通知的id,具有唯一性;第二个参数:Notification对象
    28             mgr.notify(1, notification);
    29             break;
    30 
    31         default:
    32             break;
    33         }
    34     }

      (4)最后注册NotificationActivity活动。

      3、通知的高级技巧

      (1)在发送通知的时候同时播放一段音频:只用在发送通知按钮的点击事件中加入如下代码:

    1        ...
    2        // +.在发送通知的时候播放一段音频,这里的路径是手机默认来电铃声
    3             Uri soundUri = Uri.fromFile(new File(
    4                     "/system/media/audio/ringtones/Basic_tone.ogg"));
    5             notification.sound = soundUri;
    6 
    7             // 4.使用NotificationManager的notify方法让通知显示出来
    8             // 第一个参数:通知的id,具有唯一性;第二个参数:Notification对象
    9             mgr.notify(1, notification);
            ...

       Tips:用R.E管理器进入/system/media/audio,里面有四个文件夹,分别是alarms(闹钟铃声),notifications(通知即短信铃声),ringtones(来电铃声),ui(一些应用程序操作的效果声音比如拍照等)。

      (2)发送通知时让手机振动:

     1        ...
     2        // +.发送通知时让手机振动:
     3             // 说明:这个数组下标为0的值表示手机静止的时长(毫秒),下标为1的表示振动的时长,下标为2的表示静止的时长,...以此类推
     4             // 让手机振动需要申请权限:<uses-permission
     5             // android:name="android.permission.VIBRATE" />
     6             long[] vibrates = { 0, 1000, 1000, 1000 };
     7             notification.vibrate = vibrates;
     8 
     9             // 4.使用NotificationManager的notify方法让通知显示出来
    10             // 第一个参数:通知的id,具有唯一性;第二个参数:Notification对象
    11             mgr.notify(1, notification);
    12        ...

      (3)控制LED灯亮

     1             ...
     2             // +.发送通知的时候让LED灯亮
     3             notification.ledARGB = Color.GREEN; // 控制亮灯的颜色,一般可以选红绿蓝三种颜色
     4             notification.ledOnMS = 1000; // 灯亮的时长(毫秒)
     5             notification.ledOffMS = 2000; // 灯暗去的时长,在手机在锁屏状态且用户查看通知之前,灯会交替亮、暗下去
     6             notification.flags = Notification.FLAG_SHOW_LIGHTS; // 指定通知的行为,这里是亮灯
    7 // 4.使用NotificationManager的notify方法让通知显示出来 8 // 第一个参数:通知的id,具有唯一性;第二个参数:Notification对象 9 mgr.notify(1, notification); 10 ...

      (4)通知的铃声、振动、灯光的设置使用系统默认值:

    1             // +.铃声、振动、通知的设置使用系统默认值
    2             notification.defaults = Notification.DEFAULT_ALL;
  • 相关阅读:
    lua学习项目笔记
    Jenkins 安装、配置与项目新建及构建
    Gitlab的安装及项目新建
    PHP常见排序算法
    抽象类和接口的区别
    分布式版本控制系统Git的安装及使用
    深入解析OpenCart的代理类proxy
    PHP模式设计之单例模式、工厂模式、注册树模式、适配器模式、观察者模式
    linux命令行传递参数定期执行PHP文件
    php类中的$this,static,const,self这几个关键字使用方法
  • 原文地址:https://www.cnblogs.com/jiayongji/p/5315398.html
Copyright © 2020-2023  润新知