很开心写了第二个多功能按钮使用Notification编写,实现方法与上一次的功能差不多(每一个按钮都会有相应的提示。至于方法,我都写在了Activity里面。欢迎大家下载参考)。是基础编。先让我们看一下图先。

 

android多功能按钮二——基础编_第1张图片

 

android多功能按钮二——基础编_第2张图片

Main.java代码

 

            
  1. package com.smart.activity; 
  2.  
  3. import android.app.Activity; 
  4. import android.app.Notification; 
  5. import android.app.NotificationManager; 
  6. import android.app.PendingIntent; 
  7. import android.content.Intent; 
  8. import android.os.Bundle; 
  9. import android.view.View; 
  10. import android.view.View.OnClickListener; 
  11. import android.widget.Button; 
  12.  
  13. public class Main extends Activity implements OnClickListener{ 
  14.     private NotificationManager nm; 
  15.     //使用默认的方法 
  16.     private void setDefaults(String tickerText,String contentTitile,String contentText,int id,int resId,int defaults){ 
  17.         Notification notification=new Notification(resId,tickerText,System.currentTimeMillis()); 
  18.         PendingIntent conIntent=PendingIntent.getActivity(this0new Intent(this,Main.class), 0); 
  19.         notification.setLatestEventInfo(this, contentTitile, contentText, conIntent); 
  20.         notification.defaults=defaults; 
  21.         nm.notify(id, notification); 
  22.      
  23.      
  24.     } 
  25.      
  26.     //心情方法 
  27.    private void showNotification(String tickerText,String contentTitle,String contentText,int id,int resId){ 
  28.        Notification notification=new Notification(resId,tickerText,System.currentTimeMillis()); 
  29.        PendingIntent contentTntent=PendingIntent.getActivity(this0, getIntent(), 0); 
  30.         
  31.        notification.setLatestEventInfo(this, contentTitle, contentText, contentTntent); 
  32.        nm.notify(id,  notification); 
  33.    } 
  34.      
  35.      
  36.      
  37.  
  38.     @Override 
  39.     public void onClick(View v) { 
  40.         switch(v.getId()){//根据点击不同的按钮,弹出相应的提示 
  41.         case R.id.btnSmile: 
  42.             showNotification("情人节到了,我想你!""情人节到了,我想你!"
  43.                         "情人节到了,与你一起度过,yeah!", R.drawable.smile, 
  44.                         R.drawable.smile); 
  45.             break
  46.         case R.id.btnWhy: 
  47.             showNotification("情人节,你没送礼物给我""所以心情不好。""以后要记得"
  48.                     R.drawable.why, R.drawable.wrath); 
  49.             break
  50.         case R.id.btnClear: 
  51.             nm.cancelAll(); 
  52.             break
  53.         case R.id.btnRing: 
  54.             setDefaults("使用默认的声音""使用默认的声音""使用默认的声音", R.id.btnRing, R.drawable.smile, 
  55.                     Notification.DEFAULT_SOUND); 
  56.             break
  57.         case R.id.btnVibrate: 
  58.             setDefaults("使用默认的震动""使用默认的震动""使用默认的震动", R.id.btnVibrate, R.drawable.smile, 
  59.                     Notification.DEFAULT_SOUND); 
  60.             break
  61.         case R.id.btnLight: 
  62.             setDefaults("使用默认的Light""使用默认的Light""使用默认的Light", R.id.btnLight, R.drawable.smile, 
  63.                     Notification.DEFAULT_SOUND); 
  64.             break
  65.         case R.id.btnRingAndVibrate: 
  66.             setDefaults("所有的都使用默认值""所有的都使用默认值""所有的都使用默认值", R.id.btnRingAndVibrate, R.drawable.smile, 
  67.                     Notification.DEFAULT_SOUND); 
  68.             break
  69.              
  70.         } 
  71.     } 
  72.  
  73.      @Override 
  74.         public void onCreate(Bundle savedInstanceState) { 
  75.             super.onCreate(savedInstanceState); 
  76.             setContentView(R.layout.main); 
  77.              
  78.             nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
  79.             Button btnSmile=(Button)findViewById(R.id.btnSmile); 
  80.             Button btnWhy=(Button)findViewById(R.id.btnWhy); 
  81.             Button btnWrath=(Button)findViewById(R.id.btnWrath); 
  82.             Button btnClear=(Button)findViewById(R.id.btnClear); 
  83.             Button btnRing=(Button)findViewById(R.id.btnRing);   
  84.             Button btnVibrate=(Button)findViewById(R.id.btnVibrate); 
  85.             Button btnLight=(Button)findViewById(R.id.btnLight); 
  86.             Button btnRingAndVibrate=(Button)findViewById(R.id.btnRingAndVibrate); 
  87.             btnSmile.setOnClickListener(this); 
  88.             btnWrath.setOnClickListener(this); 
  89.             btnClear.setOnClickListener(this); 
  90.             btnRing.setOnClickListener(this); 
  91.             btnVibrate.setOnClickListener(this); 
  92.             btnLight.setOnClickListener(this); 
  93.             btnRingAndVibrate.setOnClickListener(this); 
  94.       
  95.       
  96.       
  97.      } 

 

更多相关文章

  1. 解决EventBus中接收方法中无法更新UI的问题
  2. Android .so abi兼容,通用armeabi-v7a和arm64-v8a架构的方法
  3. Android 首选网络模式默认值的修改方法
  4. Android实现渐显按钮的左右滑动效果
  5. Android学习笔记-Android非布局activity中布局文件及控件加载方
  6. Android基于OpenGL的GLSurfaceView创建一个Activity实现方法
  7. 【转】Android-Action Bar使用方法

随机推荐

  1. Android(安卓)h264 硬编码 软编码
  2. Android初始化的时候获取加载的布局的宽
  3. android 事件处理机制之requestDisallowI
  4. 疯狂Android第一章:Android环境配置以及
  5. Android(安卓)TextView的drawLeft、drawR
  6. Android如何打开/关闭系统解锁服务?—典型
  7. Android(安卓)quikboot
  8. 【quickhybrid】H5和Native交互原理
  9. 安卓unit与instrumentation测试教程
  10. Android异步消息处理机制完全解析