Notification

消息通知:

首先要用到NotificationManager (消息通知)管理器

public class MainActivity extends Activity {
Button m_Button1,m_Button2,m_Button3,m_Button4;
//声明通知管理器
NotificationManager m_NotificationManager;
Intent m_Intent;
PendingIntent pendingIntent;

//声明Notification 对象
Notification m_Notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//初始化NotificationManager对象
m_NotificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
m_Button1=(Button) findViewById(R.id.Button01);
m_Button2=(Button) findViewById(R.id.Button02);
m_Button3=(Button) findViewById(R.id.Button03);
m_Button4=(Button) findViewById(R.id.Button04);
m_Intent=new Intent(MainActivity.this, Activity2.class);

//设置点击通知时显示的内容
pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, m_Intent, 0);
m_Notification=new Notification();
m_Button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {

//设置通知在状态栏显示的图标
m_Notification.icon=R.drawable.img1;

//当我们点击通知时显示的内容
m_Notification.tickerText="button1通知内容。。。。。。";

//通知时发出默认的声音
m_Notification.defaults=Notification.DEFAULT_SOUND;

//设置通知显示的参数
m_Notification.setLatestEventInfo(getApplicationContext(), "button1", "button1通知", pendingIntent);

//可以理解为执行这个通知
m_NotificationManager.notify(0, m_Notification);
}
});
m_Button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
m_Notification.icon=R.drawable.img2;
m_Notification.tickerText="button2通知内容。。。。。。";

//通知时震动
m_Notification.defaults=Notification.DEFAULT_VIBRATE;
m_Notification.setLatestEventInfo(getApplicationContext(), "button2", "button2通知", pendingIntent);
m_NotificationManager.notify(0, m_Notification);
}
});
m_Button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
m_Notification.icon=R.drawable.img3;
m_Notification.tickerText="button3通知内容。。。。。。";

//通知时屏幕发亮
m_Notification.defaults=Notification.DEFAULT_LIGHTS;
m_Notification.setLatestEventInfo(getApplicationContext(), "button3", "button3通知", pendingIntent);
m_NotificationManager.notify(0, m_Notification);
}
});
m_Button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
m_Notification.icon=R.drawable.img4;
m_Notification.tickerText="button4通知内容。。。。。。";

//通知时屏幕震动,亮,声音
m_Notification.defaults=Notification.DEFAULT_ALL;
m_Notification.setLatestEventInfo(getApplicationContext(), "button4", "button4通知", pendingIntent);
m_NotificationManager.notify(0, m_Notification);

}
});
}
}
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值