需求:
定义一个通知栏消息,点击可以跳转到二级界面。
MainActivity:
package org.mobiletrain.notification_demo; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button sendButton,cancelButton; NotificationManager nm; final int NOTIFICATION_ID = 0X100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendButton = (Button)findViewById(R.id.sendBn); cancelButton = (Button)findViewById(R.id.cancelBn); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub send(); } }); cancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub cancel(); } }); } //发送通知信息 @SuppressLint("NewApi") private void send(){ //写出即将跳转组件的“意图” Intent intent = new Intent(MainActivity.this,SecondActivity.class); //封装一个Pending对象,用来封装将要发生的事。 PendingIntent pIntent = PendingIntent.getActivity (MainActivity.this, 0, intent, 0); Notification.Builder builder = new Notification.Builder(MainActivity.this); builder.setAutoCancel(true); //设置通知栏点击后就消失。 builder.setTicker("您有新消息啦!"); //设置提示信息 builder.setSmallIcon(android.R.drawable.ic_menu_add); //设置通知的图标 builder.setContentTitle("有一条新消息"); //设置通知的标题 builder.setContentText("您中了500万,点击查看详情");//设置通知的内容 builder.setDefaults(Notification.DEFAULT_ALL); //设置声音,振动,led灯等操作 builder.setWhen(System.currentTimeMillis()); //设置发送时间 builder.setContentIntent(pIntent); //设置将要发生的跳转组件的意图 Notification notification = builder.getNotification();//获取通知对象 nm.notify(NOTIFICATION_ID, notification); //发送通知 // } private void cancel(){ nm.cancel(NOTIFICATION_ID); //取消通知 } }
<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=".MainActivity" > <Button android:id="@+id/sendBn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="发送通知" /> <Button android:id="@+id/cancelBn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/sendBn" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" 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" android:padding="20dp" android:background="#f00" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="中大奖了!!!" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="恭喜您被《非常6+1》选为幸运观众,中了500万大奖!!!!" /> </LinearLayout>