安卓消息通知栏

  1. package jason.notification;

  2. import android.app.Activity;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.Menu;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;

  13. public class MainActivity extends Activity {
  14.         Button button;int id = 0;
  15.         @Override
  16.         protected void onCreate(Bundle savedInstanceState) {
  17.                 super.onCreate(savedInstanceState);
  18.                 setContentView(R.layout.activity_main);
  19.                 button = (Button) findViewById(R.id.notify);
  20.                 button.setOnClickListener(new OnClickListener() {
  21.                         
  22.                         @Override
  23.                         public void onClick(View arg0) {
  24.                                 //获得通知管理器
  25.                                 NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  26.                                 //构建一个通知对象(需要传递的参数有三个,分别是图标,标题和 时间)
  27.                                 Notification notification = new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());
  28.                                 Intent intent = new Intent(MainActivity.this,MainActivity.class);
  29.                                 PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);                                                                          notification.setLatestEventInfo(getApplicationContext(), "通知标题", "通知显示的内容", pendingIntent);
  30.                                 notification.flags = Notification.FLAG_AUTO_CANCEL;//点击后自动消失
  31.                                 notification.defaults = Notification.DEFAULT_SOUND;//声音默认
  32.                                 manager.notify(id, notification);//发动通知,id由自己指定,每一个Notification对应的唯一标志
  33.                                 //其实这里的id没有必要设置,只是为了下面要用到它才进行了设置

  34.                         }
  35.                 });
  36.         }
  37. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值