今天来接触一下android里的通知:
- 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,好了,完成。