Android通知Notification
来源:互联网 发布:按网络功能层次分 编辑:程序博客网 时间:2024/05/17 01:21
点击 发送通知 按钮,则发送通知到设备的通知栏。点击 清除通知 则清除通知栏上的消息通知。
package com.example.notification;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RemoteViews;public class MainActivity extends Activity {private int NOTIFICATION_ID = 100;private int requestCode = 0000;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button send = (Button) findViewById(R.id.button1);send.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {sendmessge();}});Button cleanr = (Button) findViewById(R.id.button2);cleanr.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {cleanr();}});}public void sendmessge() {NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(this);mbuilder.setSmallIcon(R.drawable.ic_launcher);// mbuilder.setContentTitle("你有新消息了");// mbuilder.setContentText("成都爆炸事件据官方消息调查通知");Notification notifocation = mbuilder.build();// 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局RemoteViews rv = new RemoteViews(this.getPackageName(), R.layout.notification);rv.setImageViewResource(R.id.imageview, R.drawable.ic_launcher);rv.setTextViewText(R.id.textview1, "腾讯消息讯。。");rv.setTextViewText(R.id.textview2, "成都爆炸音又来新消息了");notifocation.contentView = rv;// 缺省设置为当发送通知到通知栏时候:提示声音 + 手机震动notifocation.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;// 通知的时间notifocation.when = System.currentTimeMillis();// 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。Intent intent = new Intent(this, MainActivity.class);// 当用户点击通知栏的Notification时候,切换回MainActivity。PendingIntent pi = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);notifocation.contentIntent = pi;// 发送到手机的通知栏notificationManager.notify(NOTIFICATION_ID, notifocation);}public void cleanr() {NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);notificationManager.cancel(NOTIFICATION_ID);}}需要的activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.notification.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="23dp" android:layout_marginTop="34dp" android:text="发送通知" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="83dp" android:text="清除消息" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
我的第二个布局写的不是很好。信息显示不全。自己可以拿来更改一下你的布局 用相对布局方便一点儿。线性布局显示不全信息。暂时还没有修改
0 0
- Notification---Android Notification通知详解
- 定制android通知(Notification)
- 定制android通知(Notification)
- android之Notification通知
- android之Notification通知
- Android 消息通知-Notification
- Android Notification通知详解
- android之Notification通知
- Android通知Notification
- Android 通知栏 Notification
- Android Notification通知详解
- android之Notification通知
- Android 状态栏通知Notification
- android之Notification通知
- android之Notification通知
- Android Notification通知详解
- Android之Notification通知
- android之Notification通知
- mysql数据库的备份与还原
- (EM算法)The EM Algorithm
- java处理数字格式的几种方式
- webAPP项目基础知识介绍用html5+css3+js开发
- Appcan listview上拉加载下拉刷新源码
- Android通知Notification
- 混合高斯模型(Mixtures of Gaussians)和EM算法
- POJ1276 多重背包DP 生命不息优化不止
- jiffies使用举例
- Linux 用户环境变量丢失故障及解决
- 支持向量机(1)
- 黑马程序员——JAVA基础——IO流
- java.net.BindException: Address already in use: JVM_Bind:8080
- (C++)UrlEncode的标准实现 libcul