首页 > 移动平台 > 详细

Android多媒体——通知持续完善

时间:2015-10-29 19:46:43      阅读:307      评论:0      收藏:0      [点我收藏+]

阶段一:初识通知

发送通知:通过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 已然过时,虽然可用

 

 

 

 

 

 

  

Android多媒体——通知持续完善

原文:http://www.cnblogs.com/erhai/p/4921331.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!