栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android开发之Notification手机状态栏通知用法实例分析

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android开发之Notification手机状态栏通知用法实例分析

本文实例讲述了Android开发之Notification手机状态栏通知用法。分享给大家供大家参考,具体如下:

简介:

通知是显示在手机状态栏的通知(PS:就是手机上方,显示时间啥的那一栏)

用法:

Notification添加了Builder()类,其包含如下方法:

1. setDefaults()         通知led灯、音乐、震动等

2. setAutoChange()  设置点击通知后,通知自动从状态栏删除

3. setContentTitle()   通知标题

4. setContentText()  通知内容

5. setSmallcon()      为通知设置图标

6. setLargelcon()       为通知设置大图标

7. setTick()               设置通知状态栏的提示文本

8. setContentIntent()点击通知后要启动的相应组件

运行效果:

实现方法:

1.首先建立一个活动用来执行:

public class MainActivity extends Activity {
  static final int NOTIFICATION_ID = 0x123;
  NotificationManager notificationManager;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //获取系统的Notification对象
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
   }
  //为发送通知的按钮点击事件定义事件处理方法
  @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
  public void send(View source){
    //创建一个其他Activity的Intent
    Intent intent = new Intent(MainActivity.this,TextActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
    Notification notification = new Notification.Builder(this)
 //设置打开通知 通知自动消失
 .setAutoCancel(true)
 //设置显示状态栏的通知提示信息
 .setTicker("注目提醒!")
 //设置通知图标
 .setSmallIcon(R.drawable.seek02)
 //设置通知内容标题
 .setContentTitle("该应用发生 爆炸大 大 大 新闻!!")
 //设置通知内容
 .setContentText("冒险没有 你手机自嗨罢了~")
 //设置使用默认的声音 LED灯
 .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS)
 //设置通知自定义声音
// .setSound()
 .setWhen(System.currentTimeMillis())
 //设置他只要启动的程序Intent
 .setContentIntent(pendingIntent)
 .build();
    notificationManager.notify(NOTIFICATION_ID,notification);
  }
  public void del(View view){
    //取消通知
    notificationManager.cancel(NOTIFICATION_ID);
  }
}

2.然后建立一个要打开的活动(随意建就行)(布局文件任意我这里就不写了)

public class TextActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_t_exta_ctivity);
  }
}

最后记得添加权限(mainfest)





PS:关于Android权限控制可参考~
Android Manifest功能与权限描述大全: http://tools.jb51.net/table/AndroidManifest

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/154946.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号