Android入门笔记 - 界面开发 - Notification, NotificationManager
2014-12-04 10:24
363 查看
今天来接触一下android里的通知:
Notification
NotificationManager
里面涉及到两个activity,所以又两个layout文件,一个是主程序的,一个是点击通知栏图标之后弹出的activity,直接上代码:
这个实例很简单,就是点击Button1按钮,然后会在屏幕上方有一个通知,点击通知之后跳转到宁外一个页面。
关键使用到的组件是:NotificationManager 和 Notification, PendingIntent是对Intent的扩展。我们来看一看用法:
最后就是manager来notify一个Notification,好了,完成。
Notification
NotificationManager
里面涉及到两个activity,所以又两个layout文件,一个是主程序的,一个是点击通知栏图标之后弹出的activity,直接上代码:
1. res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击按钮,发送通知"/> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1"/> </LinearLayout>
2. res/layout/activity_notification.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:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通知消息界面" android:textSize="30sp" /> </LinearLayout>
3. src/MainActivity.java
package com.example.demoui5_notification; import android.os.Bundle; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Context ctx = this; private Button mBtn1; private NotificationManager notificationManager; private Notification notification; private PendingIntent pendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(MainActivity.this, NotificationActivity.class); pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0); notification = new Notification(); mBtn1 = (Button) findViewById(R.id.btn1); mBtn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { notification.icon = R.drawable.ic_launcher; notification.tickerText = "Button1的通知消息"; notification.defaults = Notification.DEFAULT_SOUND; notification.setLatestEventInfo(ctx, "Button1", "Button1通知", pendingIntent); notificationManager.notify(0, notification); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
4. src/ NotificationActivity.java
package com.example.demoui5_notification; import android.app.Activity; import android.os.Bundle; public class NotificationActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); } }
这个实例很简单,就是点击Button1按钮,然后会在屏幕上方有一个通知,点击通知之后跳转到宁外一个页面。
关键使用到的组件是:NotificationManager 和 Notification, PendingIntent是对Intent的扩展。我们来看一看用法:
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(MainActivity.this, NotificationActivity.class); pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0); notification = new Notification(); mBtn1 = (Button) findViewById(R.id.btn1); mBtn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { notification.icon = R.drawable.ic_launcher; notification.tickerText = "Button1的通知消息"; notification.defaults = Notification.DEFAULT_SOUND; notification.setLatestEventInfo(ctx, "Button1", "Button1通知", pendingIntent); notificationManager.notify(0, notification); } });先通过 getSystemService(NOTIFICATION_SERVICE)得到通知管理者,由于点击通知发送的intent与直接在activity里发送intent是不一样的,所以这里用到了PendingIntent,构造方式如上。 然后就是构造通知对象(Notification),可以设置icon,标题栏显示的文字,声音,以及下拉标题栏时显示的通知样式,还需要将pendingIntent与其绑定。
最后就是manager来notify一个Notification,好了,完成。
相关文章推荐
- Android入门笔记 - 界面开发 - ProgressDialog
- Android入门笔记 - 界面开发 - 帧动画
- Android入门笔记 - 界面开发 - ProgressBar,Handler
- Android入门笔记 - 界面开发 - TextView,Button,EditText,Toast
- Android入门笔记 - 界面开发 - GridView, BaseAdapter
- Android入门笔记 - 界面开发 - Animation
- Android入门笔记 - 界面开发 - RadioButton,CheckBox,Spinner,AutoCompleteTextView,DatePicker,TimePicker
- Android入门笔记 - 界面开发 - Menu,Dialog
- Android开发学习笔记(5):Notification和NotificationManager
- Android 开发笔记——环境入门
- Android 开发学习笔记(五)—— 最简单的注册界面
- Android 开发入门指南 学习笔记
- android ->在界面上使用URI编程 ----开发笔记1
- Android 开发笔记——环境入门
- Android 开发笔记之界面开发
- Android开发笔记--制作启动界面splash
- Android开发入门必知:应用界面组成
- android开发入门笔记--环境安装
- android ->在界面上使用URI编程 ----开发笔记1
- Android开发入门学习笔记之三