Android——状态栏通知栏Notification
1、AndroidManifest.xml注意要同时注册Notification02Activity
<!-- 状态通知栏 Notification -->
<activity
android:name="com.example.notification.Notification01Activity"
android:label="状态通知" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.notification.Notification02Activity"
android:label="Notification01Activity" >
</activity>
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
<?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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="状态栏提示演示协助实现界面!" />
</LinearLayout>
5、activity_notification01.xml
<?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" >
<Button
android:id="@+id/btn1_notifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button01" />
<Button
android:id="@+id/btn2_notifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button02" />
<Button
android:id="@+id/btn3_notifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button03" />
<Button
android:id="@+id/btn4_notifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button04" />
</LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。