状态栏通知 android,Android——状态栏通知栏Notification

1、AndroidManifest.xml注意要同时注册Notification02Activity

android:name="com.example.notification.Notification01Activity"

android:label="状态通知" >

android:name="com.example.notification.Notification02Activity"

android:label="Notification01Activity" >

2、Notification01Activity.java

public class Notification01Activity extends Activity {

Button button01, button02, button03, button04;

// 声明通知(消息)管理器

NotificationManager mNotificationManager;

Intent mIntent;

PendingIntent mPendingIntent;

// 声明Notification对象

Notification mNotification;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_notification01);

// 获得四个按钮对象

button01 = (Button) findViewById(R.id.btn1_notifi);

button02 = (Button) findViewById(R.id.btn2_notifi);

button03 = (Button) findViewById(R.id.btn3_notifi);

button04 = (Button) findViewById(R.id.btn4_notifi);

// 初始化NotificationManager对象*************

mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// 点击通知时转移内容*********查看通知的具体内容

mIntent = new Intent(Notification01Activity.this,

Notification02Activity.class);

// 主要是设置点击通知时显示内容的类***********

mPendingIntent = PendingIntent.getActivity(Notification01Activity.this,

0, mIntent, 0);

// 构造Notification对象

mNotification = new Notification();

button01.setOnClickListener(new Button.OnClickListener() {

@Override

public void onClick(View v) {

/* 状态栏上的通知图标及内容 */

// 设置通知在状态栏显示的图标

mNotification.icon= R.drawable.button1;

// 当我们点击通知时显示的内容

mNotification.tickerText = "Button01通知内容……";

// 通知时发出默认声音

mNotification.defaults = Notification.DEFAULT_SOUND;

/* ==============展开状态栏的快讯=========== */

// 设置通知显示的参数

mNotification.setLatestEventInfo(Notification01Activity.this,

"Button01", "Button01通知", mPendingIntent);

// 可理解为执行这个通知

mNotificationManager.notify(0, mNotification);

}

});

button02.setOnClickListener(new Button.OnClickListener() {

@Override

public void onClick(View arg0) {

mNotification.icon = R.drawable.button2;

mNotification.tickerText = "Button02通知内容……";

mNotification.defaults = Notification.DEFAULT_SOUND;

mNotification.setLatestEventInfo(Notification01Activity.this,

"Button02", "Button02通知", mPendingIntent);

mNotificationManager.notify(0, mNotification);

}

});

button03.setOnClickListener(new Button.OnClickListener() {

@Override

public void onClick(View v) {

mNotification.icon = R.drawable.button3;

mNotification.defaults = Notification.DEFAULT_SOUND;

mNotification.tickerText = "Button03通知内容……";

mNotification.setLatestEventInfo(Notification01Activity.this,

"Button03", "Button03通知", mPendingIntent);

mNotificationManager.notify(0, mNotification);

}

});

button04.setOnClickListener(new Button.OnClickListener() {

@Override

public void onClick(View v) {

mNotification.icon = R.drawable.button31;

mNotification.defaults = Notification.DEFAULT_SOUND;

mNotification.tickerText = "Button04通知内容……";

mNotification.setLatestEventInfo(Notification01Activity.this,

"Button04", "Button04通知", mPendingIntent);

mNotificationManager.notify(0, mNotification);

}

});

}

}

3、Notification02Activity.java

public class Notification02Activity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//这里直接限制一个TextView

setContentView(R.layout.activity_notification02);

}

}

4、activity_notification02.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="状态栏提示演示协助实现界面!" />

5、activity_notification01.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/btn1_notifi"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button01" />

android:id="@+id/btn2_notifi"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button02" />

android:id="@+id/btn3_notifi"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button03" />

android:id="@+id/btn4_notifi"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button04" />

原文:http://www.cnblogs.com/Defry/p/4413928.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值