- Android 基础
- Android - 首页
- Android - 概述
- Android - 环境搭建
- Android - 架构
- Android - 应用组件
- Android - Hello World 示例
- Android - 资源
- Android - 活动(Activity)
- Android - 服务(Service)
- Android - 广播接收器(Broadcast Receiver)
- Android - 内容提供器(Content Provider)
- Android - 碎片(Fragment)
- Android - 意图/过滤器(Intents/Filters)
- Android - 用户界面
- Android - UI 布局
- Android - UI 控件
- Android - 事件处理
- Android - 样式和主题
- Android - 自定义组件
- Android 高级概念
- Android - 拖放
- Android - 通知
- 基于位置的服务
- Android - 发送邮件
- Android - 发送短信
- Android - 电话呼叫
- 发布 Android 应用
- Android 实用示例
- Android - 警报对话框
- Android - 动画
- Android - 音频捕捉
- Android - 音频管理器
- Android - 自动完成
- Android - 最佳实践
- Android - 蓝牙
- Android - 相机
- Android - 剪贴板
- Android - 自定义字体
- Android - 数据备份
- Android - 开发者工具
- Android - 模拟器
- Android - Facebook 集成
- Android - 手势
- Android - Google 地图
- Android - 图片特效
- Android - ImageSwitcher
- Android - 内部存储
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 集成
- Android - 加载旋转器
- Android - 本地化
- Android - 登录屏幕
- Android - MediaPlayer
- Android - 多点触控
- Android - 导航
- Android - 网络连接
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 进度圆圈
- Android - 进度条
- Android - 推送通知
- Android - RenderScript
- Android - RSS 阅读器
- Android - 屏幕录制
- Android - SDK 管理器
- Android - 传感器
- Android - 会话管理
- Android - 共享首选项
- Android - SIP 协议
- Android - 拼写检查器
- Android - SQLite 数据库
- Android - 支持库
- Android - 测试
- Android - 文字转语音
- Android - TextureView
- Android - Twitter 集成
- Android - UI 设计
- Android - UI 模式
- Android - UI 测试
- Android - WebView 布局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 实用资源
- Android - 问答
- Android - 实用资源
- Android - 讨论
Android - 推送通知
通知是在应用程序正常 UI 之外显示给用户的消息。您可以在 Android 中非常轻松地创建自己的通知。
Android 为此目的提供了NotificationManager 类。为了使用此类,您需要通过getSystemService() 方法请求 Android 系统来实例化此类的对象。其语法如下所示:
NotificationManager NM; NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
之后,您将通过Notification 类创建通知并指定其属性,例如图标、标题和时间等。其语法如下所示:
Notification notify = new Notification(android.R.drawable.stat_notify_more,title,System.currentTimeMillis());
接下来需要做的是通过传递上下文和意图作为参数来创建PendingIntent。通过向另一个应用程序提供 PendingIntent,您授予它执行您指定的 operatio 的权限,就好像另一个应用程序是自己一样。
PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(),0);
最后需要做的是调用 Notification 类的setLatestEventInfo 方法,并将 pending intent 与通知主题和正文详细信息一起传递。其语法如下所示。然后最后调用 NotificationManager 类的 notify 方法。
notify.setLatestEventInfo(getApplicationContext(), subject, body,pending); NM.notify(0, notify);
除了 notify 方法之外,NotificationManager 类中还提供了其他方法。它们列在下面:
| 序号 | 方法和描述 |
|---|---|
| 1 |
cancel(int id) 此方法取消先前显示的通知。 |
| 2 |
cancel(String tag, int id) 此方法也取消先前显示的通知。 |
| 3 |
cancelAll() 此方法取消所有先前显示的通知。 |
| 4 |
notify(int id, Notification notification) 此方法发布要在状态栏中显示的通知。 |
| 5 |
notify(String tag, int id, Notification notification) 此方法还在状态栏中发布要显示的通知。 |
示例
以下示例演示了 NotificationManager 类的用法。它创建了一个基本的应用程序,允许您创建通知。
要试验此示例,您需要在实际设备或模拟器上运行它。
| 步骤 | 描述 |
|---|---|
| 1 | 您将使用 Android Studio 在包 com.example.sairamkrishna.myapplication 下创建一个 Android 应用程序。 |
| 2 | 修改 src/MainActivity.java 文件以添加通知代码。 |
| 3 | 修改布局 XML 文件 res/layout/activity_main.xml,如果需要添加任何 GUI 组件。 |
| 4 | 运行应用程序并选择一个正在运行的 Android 设备,并在其上安装应用程序并验证结果。 |
以下是MainActivity.java的内容。
在以下代码中,abc表示 tutorialspoint.com 的徽标
package com.example.sairamkrishna.myapplication;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
EditText ed1,ed2,ed3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
Button b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tittle=ed1.getText().toString().trim();
String subject=ed2.getText().toString().trim();
String body=ed3.getText().toString().trim();
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle(tittle).setContentText(body).
setContentTitle(subject).setSmallIcon(R.drawable.abc).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
});
}
}
以下是activity_main.xml的内容
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:textColor="#ff16ff01" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_marginTop="52dp"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:hint="Name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:hint="Subject"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText3"
android:hint="Body"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_alignEnd="@+id/editText2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification"
android:id="@+id/button"
android:layout_marginTop="77dp"
android:layout_below="@+id/editText3"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
</RelativeLayout>
以下是AndroidManifest.xml的内容。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
让我们尝试运行我们的应用程序。要从 Android Studio 运行应用程序,请打开项目的一个活动文件,然后单击工具栏中的运行
图标。在启动应用程序之前,Android Studio 将显示以下窗口以选择要在其中运行 Android 应用程序的选项。
现在填写标题、主题和正文字段。这已在下图中显示:
现在单击通知按钮,您将在顶部通知栏中看到一个通知。这已在下图中显示:
现在向下滚动通知栏并查看通知。这已在下图中显示:
