小记:安卓发送通知栏通知

 1 package com.lixu.tongzhi;
 2
 3 import android.app.Activity;
 4 import android.app.Notification;
 5 import android.app.NotificationManager;
 6 import android.app.PendingIntent;
 7 import android.content.Intent;
 8 import android.os.Bundle;
 9 import android.support.v4.app.NotificationCompat;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.widget.Button;
13 import android.widget.RemoteViews;
14 import android.widget.Toast;
15
16 public class MainActivity extends Activity {
17
18     private static final int ID = 1987;
19     private static final int REQUEST_CODE = 123;
20
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25
26         Button btn1 = (Button) findViewById(R.id.fasong);
27         Button btn2 = (Button) findViewById(R.id.qingchu);
28
29         btn1.setOnClickListener(new OnClickListener() {
30
31             @Override
32             public void onClick(View v) {
33                 send();
34
35                 Toast.makeText(getApplicationContext(), "发送通知成功!", 0).show();
36
37             }
38         });
39
40         btn2.setOnClickListener(new OnClickListener() {
41
42             @Override
43             public void onClick(View v) {
44
45                 delete();
46
47                 Toast.makeText(getApplicationContext(), "清除通知成功!", 0).show();
48
49             }
50         });
51
52     }
53
54     private void send() {
55         // 获取通知管理器
56         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
57
58         NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
59         // 设置通知栏图片
60         mBuilder.setSmallIcon(R.drawable.sdfdf);
61
62         Notification notification = mBuilder.build();
63         // RemoteViews中自定义Notification布局
64
65         RemoteViews cv = new RemoteViews(getApplicationContext().getPackageName(), R.layout.list);
66         // 设置下拉后通知栏图片
67         cv.setImageViewResource(R.id.image, R.drawable.meimei);
68         // 设置内容
69         cv.setTextViewText(R.id.tv1, "我是小超人");
70         cv.setTextViewText(R.id.tv2, "我是小超人,主人有什么事情要吩咐。");
71         notification.contentView = cv;
72
73         // 设置通知声音或者震动或闪光。
74         notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
75         // 通知时间
76         notification.when = System.currentTimeMillis();
77
78         // 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
79         Intent intent = new Intent(getApplicationContext(), MainActivity.class);
80         // 当用户点击通知栏的Notification时候,切换回MainActivity。
81         PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), REQUEST_CODE, intent,
82                 PendingIntent.FLAG_UPDATE_CURRENT);
83         notification.contentIntent = pi;
84
85         // 通过特定id来发送这个通知
86         manager.notify(ID, notification);
87
88     }
89
90     private void delete() {
91         // 获取通知管理器
92         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
93         manager.cancel(ID);
94
95     }
96
97 }
时间: 2024-08-18 00:14:48

小记:安卓发送通知栏通知的相关文章

Android发送通知栏通知

1 /** 2 * 发送通知 3 * 4 * @param message 5 */ 6 @SuppressWarnings("deprecation") 7 @SuppressLint("NewApi") 8 private void SendNotification(String message) { 9 // 点击之后执行的Intent 10 Intent intent = new Intent(mContext, MainActivity.class); 1

使用Google Cloud Messaging (GCM),PHP 开发Android Push Notifications (安卓推送通知)

什么是GCM? Google Cloud  Messaging (GCM) 是Google提供的一个服务,用来从服务端向安卓设备发送推送通知. GCM分为客户端和服务端开发. 这里我们只介绍服务端开发.其实过程非常简单,只需利用PHP发送POST数据. api key的取得? 待补充? class GCM { public $api_key = "AIzaSyAU3wZs9raik-mHQ"; function __construct() { } /** * Sending Push

安卓学习之通知(Notification)

安卓中创建通知需要借助NotificationManager来对通知进行管理.可以调用Context的getsSystemService()方法来获得. getsSystemService()方法接收一个参数,这个参数是字符串,用于指定哪一个服务.Context.NOTIFICATION_SERVICE 就是指定通知服务. 这个方法返回一个Object对象,所欲需要进行强制转换. NotificationManager manager = (NotificationManager) getSys

Android 使用 RemoteViews 发送自定义通知 ,遇到 Couldn't expand RemoteViews问题总结

在RemoteViews这种调用方式中,你只能使用以下几种界面组件: Layout:     FrameLayout, LinearLayout, RelativeLayout Component:   AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView, ViewFlipper, ListView, GridView, StackView, AdapterViewFlipper SO,当

【Android】揭秘如何利用Service与线程制造安卓后台通知栏推送

安卓设备的后台通知栏推送很烦,主要是可以通过利用安卓的Service功能,在app被点击Home键或者返回键之后,依旧可以在挂载后台运行.反正Home键或者返回键只能杀死Activity而不能终结Service的,不像Windows点关闭键默认是关闭程序界面+程序线程. 本文的意义相当于在Windows中制造一条线程,或者是对Windows应用程序的关闭键进行重写,让其应用程序的关闭仅仅是隐藏界面,不杀死进程. 理论上,只要这个Service不被安卓系统因内存不足所回收,会一直持续到设备重启,或

android学习笔记---发送状态栏通知

发送消息的代码如下:     //获取通知管理器     NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);         int icon = android.R.drawable.stat_notify_chat;         long when = System.currentTimeMillis();    

Linux利用sendmail和fetion发送报警通知

一.sendmail报警通知 1.停止系统自带的sendmail服务 # /etc/init.d/postfix stop 2.安装sendmail和mailx # yum -y install sendmail mailx 3.修改sendmail配置文件 # cp /etc/mail.rc /etc/mail.rc.bak # cat >> /etc/mail.rc << EOF set [email protected] smtp=smtp.139.com  set [ema

帝国留言板管理员回复发送EMAIL通知客户

说明:修改1:e/admin/tool/ReGook.php   /*回复表单*/ 43行处添加代码 ----------------------------------------------------------------------------------------------- <input name="email" type="hidden" id="email" value="<?=$r[email]?&g

安卓发送验证码倒计时效果和自动获取验证码并填充到输入框

在注册,实名认证.修改手机号码的,我们都会用发送验证码的功能,在这里经常就会涉及到2个效果, 第一个:发送验证码倒计时效果: 第二个:自动获取接收到的短信,并且填充验证码. 先来说倒计时效果: 其实很简单,就是一个计时器.每秒刷新一次,倒计时完了.就可以重新点击获取了. <pre name="code" class="java">btn_getYzm.setEnabled(false); btn_getYzm.setBackgroundResource(