过安卓的应该对通知栏消息都很熟悉了,下面是演示通知栏消息的一个Demo,首先来看一下界面,后面是代码,解释就都放在代码里了.
java代码
package jason.notification;
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.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.notify);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//获得通知管理器
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//构建一个通知对象(需要传递的参数有三个,分别是图标,标题和 时间)
Notification notification = new Notification(R.drawable.ic_launcher, "通知", System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,new Intent(MainActivity.this,MainActivity.class),0);());//这是一个PendingIntent,关于它的使用昨天我刚写过一个,有兴趣可以去看看
notification.setLatestEventInfo(getApplicationContext(), "通知标题", "通知显示的内容", pendingIntent);//这就是对通知的具体设置了
notification.flags = Notification.FLAG_AUTO_CANCEL;//点击后自动消失
notification.defaults = Notification.DEFAULT_SOUND;//声音默认
manager.notify(0, notification);//发动通知
}
});
}
}
package jason.notification;
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.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.notify);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//获得通知管理器
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//构建一个通知对象(需要传递的参数有三个,分别是图标,标题和 时间)
Notification notification = new Notification(R.drawable.ic_launcher, "通知", System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,new Intent(MainActivity.this,MainActivity.class),0);());//这是一个PendingIntent,关于它的使用昨天我刚写过一个,有兴趣可以去看看
notification.setLatestEventInfo(getApplicationContext(), "通知标题", "通知显示的内容", pendingIntent);//这就是对通知的具体设置了
notification.flags = Notification.FLAG_AUTO_CANCEL;//点击后自动消失
notification.defaults = Notification.DEFAULT_SOUND;//声音默认
manager.notify(0, notification);//发动通知
}
});
}
}
对于通知的结构下面有个图很好的进行了标注;
以下是每个部分的说明:
1. 内容标题
2. 大型icon
3. 内容text
4. 内容info
5. 小型icon
6. 发布通知的时间。你能使用setWhen()设置一个明确的值。