zoukankan      html  css  js  c++  java
  • Android多媒体——通知持续完善

    阶段一:初识通知

    发送通知:通过NotificationManager的实例 manager.notify(通知的id,通知实例Notification);

    所以先要获取manager:NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    然后需要Notification对象:Notification notification = new Notification(R.drawable.ic_launcher, "this is ticker", System.currentTimeMillis());

                  (通知栏图标,通知栏信息,下拉时间)

    Notification下拉的信息呢:notification.setLatestEventInfo(MainActivity.this, "title","text", pendingIntent);

                  (context,下拉标题,下拉内容,点击时执行的延迟意图pendingintent)

    所以还得有pendingIntent:PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

                  (context,这里通常传入0,目的地,构造时的flag)

    pendIntent四个值:

    • FLAG_CANCEL_CURRENT:如果构建的PendingIntent已经存在,则取消前一个,重新构建一个。
    • FLAG_NO_CREATE:如果前一个PendingIntent已经不存在了,将不再构建它。
    • FLAG_ONE_SHOT:表明这里构建的PendingIntent只能使用一次。
    • FLAG_UPDATE_CURRENT:如果构建的PendingIntent已经存在,则替换它,常用

    取消通知:同样通过manager,manager.cancel(通知的id);

    给通知添加音频

    Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones"));
    notification.sound = soundUri;

    给通知设置LED灯闪烁

    notification.ledARGB = Color.RED;// 颜色红——测试只有红和绿可用,蓝色用不了
    notification.ledOffMS = 1000; // 熄灭时长
    notification.ledOnMS = 1000;// 闪光灯亮起的时长
    notification.flags = Notification.FLAG_SHOW_LIGHTS;// 一旦设置了LED,必须为flags属性添加FLAG_SHOW_LIGHTS标志位

    给通知增加震动——需要声明权限

    // 收到通知,立即震动1秒,静止一秒,再震动1秒————震动需要权限
    // <uses-permission android:name="android.permission.VIBRATE" />
    long vibrates[] = { 0, 1000, 1000, 1000 };
    notification.vibrate = vibrates;

    注:可以直接一行代码表示使用默认:notification.defaults = Notification.DEFAULT_ALL;——无需声明权限

    小代码

     1 package com.example.notificationtest;
     2 
     3 import java.io.File;
     4 
     5 import android.app.Activity;
     6 import android.app.Notification;
     7 import android.app.NotificationManager;
     8 import android.app.PendingIntent;
     9 import android.content.Intent;
    10 import android.graphics.Color;
    11 import android.net.Uri;
    12 import android.os.Bundle;
    13 import android.view.View;
    14 import android.view.View.OnClickListener;
    15 import android.widget.Button;
    16 
    17 public class MainActivity extends Activity {
    18 
    19     @Override
    20     protected void onCreate(Bundle savedInstanceState) {
    21         super.onCreate(savedInstanceState);
    22         setContentView(R.layout.activity_main);
    23         Button sendButton = (Button) findViewById(R.id.send);
    24         sendButton.setOnClickListener(new OnClickListener() {
    25 
    26             @Override
    27             public void onClick(View v) {
    28                 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    29                 /* 可以看到setLatestEventInfo这种用法已经过时 */
    30                 Notification notification = new Notification(
    31                         R.drawable.ic_launcher, "this is ticker", System
    32                                 .currentTimeMillis());
    33 
    34                 // 指明意图
    35                 Intent intent = new Intent(MainActivity.this,
    36                         TestActivity.class);
    37                 // 封装意图 ,延迟执行
    38                 PendingIntent pendingIntent = PendingIntent.getActivity(
    39                         MainActivity.this, 0, intent,
    40                         PendingIntent.FLAG_UPDATE_CURRENT);
    41 
    42                 notification.setLatestEventInfo(MainActivity.this, "title",
    43                         "text", pendingIntent);
    44 
    45                 // 高级技巧:通知的声音、震动、闪光灯
    46                 Uri soundUri = Uri.fromFile(new File(
    47                         "/system/media/audio/ringtones"));
    48                 notification.sound = soundUri;
    49 
    50                 // 收到通知,立即震动1秒,静止一秒,再震动1秒————震动需要权限
    51                 // <uses-permission android:name="android.permission.VIBRATE" />
    52                 long vibrates[] = { 0, 1000, 1000, 1000 };
    53                 notification.vibrate = vibrates;
    54 
    55                 // LED灯闪烁
    56 
    57                 notification.ledARGB = Color.RED;// 颜色红——测试只有红和绿可用,蓝色用不了
    58                 notification.ledOffMS = 1000; // 熄灭时长
    59                 notification.ledOnMS = 1000;// 闪光灯亮起的时长
    60                 notification.flags = Notification.FLAG_SHOW_LIGHTS;// 一旦一旦你设置了LED的设定,你也必须为Notification的flags属性添加FLAG_SHOW_LIGHTS标志位
    61 
    62                 manager.notify(1, notification);
    63             }
    64         });
    65     }
    66 
    67 }
    MainActivity
    1 ...    
    2 <Button 
    3         android:id="@+id/send"
    4         android:layout_height="wrap_content"
    5         android:layout_width="match_parent"
    6         android:text="send a notification"
    7         />
    8 ...
    activity_main.xml
     1 package com.example.notificationtest;
     2 
     3 import android.app.Activity;
     4 import android.app.NotificationManager;
     5 import android.os.Bundle;
     6 
     7 public class TestActivity extends Activity {
     8 
     9     @Override
    10     protected void onCreate(Bundle savedInstanceState) {
    11         // TODO Auto-generated method stub
    12         super.onCreate(savedInstanceState);
    13         setContentView(R.layout.test);
    14 
    15         // 当点击跳转至TestActivity之后,通过manager取消掉通知
    16         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    17         manager.cancel(1);
    18     }
    19 
    20 }
    TestActivity
     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     <TextView 
     7         android:layout_height="wrap_content"
     8         android:layout_width="match_parent"
     9         android:text="hello"
    10         android:gravity="center"
    11         />
    12 
    13 </LinearLayout>
    test.xml
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.example.notificationtest"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-permission android:name="android.permission.VIBRATE" />
     8 
     9     <uses-sdk
    10         android:minSdkVersion="14"
    11         android:targetSdkVersion="19" />
    12 
    13     <application
    14         android:allowBackup="true"
    15         android:icon="@drawable/ic_launcher"
    16         android:label="@string/app_name"
    17         android:theme="@style/AppTheme" >
    18         <activity
    19             android:name=".MainActivity"
    20             android:label="@string/app_name" >
    21             <intent-filter>
    22                 <action android:name="android.intent.action.MAIN" />
    23 
    24                 <category android:name="android.intent.category.LAUNCHER" />
    25             </intent-filter>
    26         </activity>
    27         <activity android:name=".TestActivity" >
    28         </activity>
    29     </application>
    30 
    31 </manifest>
    AndroidManifest

    阶段二:然而阶段一的写法在API level 11 已然过时,虽然可用

     推荐的写法如下,通过NotificationManager 来通知

     1 public class MainActivity extends Activity {
     2 
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.activity_main);
     7         
     8         // step1 获取manager
     9         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    10         
    11         // step4 创建Builder实例
    12         NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(this);
    13         // step8 设置意图intent
    14         Intent intent = new Intent(this, TestActivity.class);
    15         // step7 设置单击的intent,延迟执行意图pendingIntent
    16         PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    17         // step5 设置Builder
    18         mbuilder.setTicker("this is ticker")// 设置ticker
    19         .setWhen(System.currentTimeMillis())// 设置通知时间
    20         .setSmallIcon(R.drawable.logo)// 设置通知图标
    21         .setContentTitle("this is content title")// 设置通知标题
    22         .setContentText("this is content text")// 设置通知内容
    23         .setVibrate(new long[]{0,1000,2000,1000})// 设置震动:立即震动1s->暂停2s->再震动1s,注意震动需要声明权限
    24         .setLights(Color.BLUE, 200, 100)// 设置led闪烁灯为红色,亮0.21s停0.1s,一般设置为1s1s,注意亮屏时无效
    25         .setSound(Uri.fromFile(new File("/system/media/audio/ringtones")))// 设置提示音
    26         .setContentIntent(pendingIntent);// 设置延迟执行意图(即单击之后跳转的活动)
    27         
    28         
    29         // step3 通过NotificationCompat.Builder的实例创建notification
    30         Notification notification = mbuilder.build();
    31         
    32         // step6 给notification设置标志位
    33         notification.flags = Notification.FLAG_SHOW_LIGHTS;// 使用三色灯必须加此标志位
    34         // step2 通过manager发送通知(1代表这个通知的编号,可以通过manager.cancle(1)取消对应的通知)
    35         manager.notify(1, notification);
    36     }
    37 
    38 }

     TestActivity.java

     1 public class TestActivity extends Activity {
     2 
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.test);
     7 
     8         // 当点击跳转至TestActivity之后,通过manager取消掉通知
     9         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    10         manager.cancel(1);// 取消通知显示
    11     }
    12 
    13 }

     震动权限声明

    <uses-permission android:name="android.permission.VIBRATE" />

     如果不需要那么多繁琐的设置:

    notification.defaults = Notification.DEFAULT_ALL;

      

  • 相关阅读:
    Eletron 打开文件夹,截图
    nodejs 与 json
    drupal sql 源码解析query.inc 文件
    The maximum column size is 767 bytes (Mysql)
    php 过滤emoji
    Mysql delete操作
    Mysql update 一个表中自己的数据
    form 表单排序
    jquery parents用法
    MYSQL数据库重点:流程控制语句
  • 原文地址:https://www.cnblogs.com/erhai/p/4921331.html
Copyright © 2011-2022 走看看