程序发送和取消状态栏的通知

来源:互联网 发布:淘宝申请退货时间 编辑:程序博客网 时间:2024/06/16 09:57

代码:

package com.xie.app;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {//布局中两个buttonprivate Button btn1, btn2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn1 = (Button) findViewById(R.id.button1);btn2 = (Button) findViewById(R.id.button2);//设置监听btn1.setOnClickListener(this);btn2.setOnClickListener(this);}@Overridepublic void onClick(View v) {//通知信息的id,用来标示通知int id = 1;//通知管理NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);if (v == btn1) {//创建一个通知对象,参数:通知显示的图标,通知开始活动时显示的内容,通知上显示的时间[通知排序通过这个]Notification notification = new Notification(R.drawable.ic_launcher, "第一次看到显示",System.currentTimeMillis());//用户点击通知时自动消失notification.flags = Notification.FLAG_AUTO_CANCEL;//通知显示在正在运行那一栏中:FLAG_ONGOING_EVENT 表示运行在正在运行那一栏中  FLAG_NO_CLEAR 不能在通知栏上清除//notification.flags |= Notification.FLAG_ONGOING_EVENT;//notification.flags |= Notification.FLAG_NO_CLEAR;//创建一个intent,如果程序已经运行那么找到入口进入,不然重新打开Intent intent = new Intent();intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_LAUNCHER);//设置component,用于找哪个程序和主页面intent.setComponent(new ComponentName(this, MainActivity.class));intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//创建PendingIntent对象PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, 0);//设置最后在状态栏显示的内容和点击后的pendingIntentnotification.setLatestEventInfo(this, "标题", "通知内容", pendingIntent);//通过id发送通知manager.notify(id, notification);}else{//通过id取消通知manager.cancel(id);}}}


activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_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=".MainActivity" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginLeft="29dp"        android:layout_marginTop="85dp"        android:text="Button" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBottom="@+id/button1"        android:layout_marginLeft="32dp"        android:layout_toRightOf="@+id/button1"        android:text="Button" /></RelativeLayout>


 

 

原创粉丝点击