- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Android中通过Notification&NotificationManager实现消息通知由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径.
1、新建一个android项目 。
我新建项目的 minSdkVersion="11",targetSdkVersion="19"。也就是支持最低版本的3.0的.
2、习惯性地打开项目清单文件AndroidManifest.xml,添加一个权限:<uses-permission android:name="android.permission.VIBRATE"/> 不添加不行的.
3、在布局activity_main.xml中添加几个按钮,样子就大概这样,垂直排版的LinearLayout 。
具体代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools=
"http://schemas.android.com/tools"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:paddingBottom=
"@dimen/activity_vertical_margin"
android:paddingLeft=
"@dimen/activity_horizontal_margin"
android:paddingRight=
"@dimen/activity_horizontal_margin"
android:paddingTop=
"@dimen/activity_vertical_margin"
android:orientation=
"vertical"
tools:context=
".MainActivity"
>
<Button
android:id=
"@+id/btn_01"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"3.0以前版本的notification,用新的吧"
android:onClick=
"click"
/>
<Button
android:id=
"@+id/btn_02"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"大视图文本通知"
android:onClick=
"click"
/>
<Button
android:id=
"@+id/btn_03"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"大视图图片通知"
android:onClick=
"click"
/>
<Button
android:id=
"@+id/btn_04"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"进度条通知"
android:onClick=
"click"
/>
</LinearLayout>
|
。
4、MainActivity中的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
package com.xin.day__notificationdemo;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v.app.NotificationCompat;
import android.support.v.app.NotificationCompat.BigPictureStyle;
import android.support.v.app.NotificationCompat.BigTextStyle;
import android.support.v.app.NotificationCompat.Builder;
import android.util.Log;
import android.view.View;
public
class
MainActivity extends Activity {
//通知的唯一标识,在一个应用程序中不同的通知要区别开来
private
static
final
int
NO = x;
private
static
final
int
NO = x;
private
static
final
int
NO = x;
private
static
final
int
NO = x;
//进度条要用
private
int
progress = ;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//click方法,和xml文件中的各个按钮的onClick属性的值要一致
public
void
click(View view) {
//创建NotificationManager
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//用switch语句控制四个控件
switch
(view.getId()) {
case
R.id.btn_: {
Notification notification =
new
Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText =
"有消息了。。。"
;
Intent intent =
new
Intent(
this
, MainActivity.
class
);
PendingIntent pendingIntent = PendingIntent.getActivity(
this
, ,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(
this
,
".以前的通知"
,
"试试而已"
, pendingIntent);
notification.when = System.currentTimeMillis();
notification.defaults = Notification.DEFAULT_ALL;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.number = ;
notification.vibrate =
new
long
[]{, };
manager.notify(NO, notification);
}
break
;
case
R.id.btn_:{
//大视图文本通知
//创建消息构造器,在扩展包
NotificationCompat.Builder builder =
new
NotificationCompat.Builder(
this
);
//设置当有消息是的提示,图标和提示文字
builder.setSmallIcon(R.drawable.ic_launcher).setTicker(
"有新消息了"
);
//需要样式
BigTextStyle style =
new
BigTextStyle();
style.setBigContentTitle(
"上课通知"
);
//通知的标题
style.bigText(
"今天下午要在综B上jsp"
);
//通知的文本内容
//大视图文本具体内容
style.setSummaryText(
"这是正常的课程安排,请各位同学按时上课"
);
builder.setStyle(style);
//显示消息到达的时间,这里设置当前时间
builder.setWhen(System.currentTimeMillis());
//获取一个通知对象
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
//发送(显示)通知
//notify()第一个参数id An identifier for this notification unique within your application
//get?意思说,这个通知在你的应用程序中唯一的标识符
manager.notify(NO, notification);
}
break
;
case
R.id.btn_:{
//大视图图片通知
NotificationCompat.Builder builderPic =
new
Builder(
this
);
builderPic.setSmallIcon(R.drawable.ic_launcher).setTicker(
"新浪体育提醒"
);
//进行设置
BigPictureStyle pictureStyle =
new
BigPictureStyle();
pictureStyle.setBigContentTitle(
"新浪体育 快船VS骑士 "
);
pictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_game));
pictureStyle.setSummaryText(
" 快船VS骑士 天王山之战!!!"
);
//不要在意文字
//设置样式
builderPic.setStyle(pictureStyle);
//设置显示的时间
builderPic.setWhen(System.currentTimeMillis());
Notification notification = pictureStyle.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
//
manager.notify(NO, notification);
}
break
;
case
R.id.btn_:{
//进度条通知
final NotificationCompat.Builder builderProgress =
new
NotificationCompat.Builder(
this
);
builderProgress.setSmallIcon(R.drawable.ic_launcher).setTicker(
"进度条通知"
);
builderProgress.setProgress(, progress,
false
);
final Notification notification = builderProgress.build();
//发送一个通知
manager.notify(NO, notification);
//创建一个计时器
Timer timer =
new
Timer();
timer.schedule(
new
TimerTask(){
@Override
public
void
run() {
Log.i(
"progress"
,progress+
""
);
while
(progress <= ){
progress ++;
try
{
Thread.sleep();
}
catch
(InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//更新进度条
builderProgress.setProgress(, progress,
false
);
//再次通知
manager.notify(NO, builderProgress.build());
}
//计时器退出
this
.cancel();
//进度条退出
manager.cancel(NO);
return
;
//结束方法
}
}, );
}
break
;
default
:
break
;
}
}
}
|
5、运行:我的虚拟机版本是4.0的(api19),按住通知左(右)滑动就可以让通知小时了.
效果如下:
最后此篇关于Android中通过Notification&NotificationManager实现消息通知的文章就讲到这里了,如果你想了解更多关于Android中通过Notification&NotificationManager实现消息通知的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在尝试使用以下方法将振铃器设置为静音并将请勿打扰设置为仅优先 AudioManager myAudioMgr = (AudioManager) context.getSystemService(C
目前我通知用户为 - mNotificationManager.notify("My App Name",1212,notification); 这很好用。但是它在状态栏中只显示一个图标,尽管我在不同
请帮助我,我是 android 的新手,我需要为我的闹钟添加通知声音。我可以在哪里放置通知声音?这是我的带有通知的代码,但出现错误“NotificationManager 无法解析为变量” 应用程序强
我有一个我无法解释的问题。我有一个包含按钮的主要 Activity 。单击此按钮时,它会启动一个新 Activity ,该 Activity 会从 Internet 下载 xml 文件。下载时会有指示
我创建了一个通过 AlarmManager 启动的简单状态通知。一切正常(通知的内容、标题、点击时启动的 Activity 等)。不幸的是,当调用通知时(即,AlarmManager 关闭),将启动并
我无法获得 NotificationManager的 Notifiy接受我的论点。该方法应该采用 3 个参数。 String tag int id Notificat
问题: 手机/模拟器锁定重复的通知更新。使模拟器在锁定后恢复响应的唯一方法是按给定顺序按 Home => Menu => Lock => Home => Menu Button。 代码: 通知推送代码
我通过 NotificationManager 发布文件上传进度,但在更新其进度 UI 时卡住。 我使用缓存在类字段中的 NotificationCompat.Builder。所以进度发布非常简单:
向后兼容性有什么作用 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); 前提是
我希望能够按需从我的 Android 应用程序中获取 Activity 通知。 (其实我只需要知道有没有)我一直在寻找这种行为,似乎我只有两个选择:NotificationManager.getAct
我正在使用 notificationManager.getActiveNotifications() 获取所有状态栏通知,但它只返回应用程序锁的通知。每当在 whats 应用程序或任何其他应用程序中发
我有一种方法可以在 Android P 中打开“请勿打扰”时允许闹钟。我有一个开关按钮可以打开闹钟并且一切正常,但问题是我不知道使用什么方法来切换警报关闭。 这是我的代码: private v
有没有办法注册接收非系统应用程序从通知管理器发出的通知? 例如,应用程序A通过NotificationManager发送通知,应用程序B可以收到通知但不干涉,然后处理通知中包含的信息。 我知道这种类型
我正在开发 GPS 应用程序。如果用户进入或超出预定义区域,我的应用程序将通过 NotificationManager 触发通知。我的应用可以针对这两种情况触发通知。 从 onResume(),我总是
我有一个服务在后台运行,如果有新数据我想通知用户,我使用了一个通知管理器,通知出现了,但是当点击它时它没有做任何事情(它是应该显示一个 Activity 我是 android 新手,这是我的代码 No
我正在同时开发两个相互通信的 Android 应用程序,并且我正在使用通知来显示收到的消息。这是我用来显示通知的代码: private void showNotification(String tit
这是我的发送通知方法,但问题是我收到通知但无法取消它。我希望我的通知在 15 分钟后以及用户点击通知后被删除。 alarmNotificationManager = (Notification
我正在尝试制作离线推送通知,该通知将在特定时间推送一次。以下是我的日历和 AlarmManager 设置: Intent intent = new Intent(getApplicationConte
我可以这样创建通知: NotificationManager notificationManager = (NotificationManager) getSystemService(Context
有谁知道有什么方法可以通过 id 获取通知吗?如果它仍然显示在 Android 的状态栏中,我想要在收到新通知时想要获取信息并将其添加到新通知中。谢谢。 最佳答案 NotificationManage
我是一名优秀的程序员,十分优秀!