码迷,mamicode.com
首页 > 其他好文 > 详细

Notification的学习,4.0前后的差别

时间:2015-08-20 10:15:07      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

android 4.0 前后很多api都有了较大的差别,不多说现在学习下notification前后实现的差别

 

 

public class MainActivity extends ActionBarActivity {
	private static final int NOTIFY_ID = 0; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
     public void tell(View v)
    {
  //  **************android 4.0后*************************************
    	String title="mynotice";//通知栏的标题
    	String store="hahagaga";
    	 NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
    	//intent是点击notification后激活的意图
    	 Intent intent = new Intent(this,MainActivity.class);  
//    	 注意:如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
//    	 Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的
//    	                 所有Activity都关掉,并把此Activity置前以避免创建Activity的实例
//    	 Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,
//    	                若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建
//    	         Activity。更多请参见 “ (转载)Android下Affinities和Task ”
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
         intent.putExtra("store", store);  
         
         //contentIntent是修饰intent的
//         Intent :意图,即告诉系统我要干什么,然后系统根据这个Intent做对应的事。如startActivity相当于发送消息,
//         			而Intent是消息的内容。
//         PendingIntent :包装Intent,Intent 是我们直接使用 startActivity , startService 或 sendBroadcast
//         启动某项工作的意图。而某些时候, 我们并不能直接调用startActivity , startServide 或 sendBroadcast ,
//         而是当程序或系统达到某一条件才发送Intent。如这里的Notification,当用户点击Notification之后,由系统发
//         出一条Activity 的 Intent 。因此如果我们不用某种方法来告诉系统的话,系统是不知道是使用 startActivity,
//         startService 还是 sendBroadcast 来启动Intent 的(当然还有其他的“描述”),因此这里便需要PendingIntent。
         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT  
   
         Notification notification = new Notification.Builder(getApplicationContext())           
                             .setContentTitle(title)  
                             .setContentText(store)  
                             .setContentIntent(contentIntent)  
                             .setSmallIcon(R.drawable.hahagaga)   
                             .setAutoCancel(true) 
                             .setWhen(System.currentTimeMillis())  
//                             .setDefaults(Notification.DEFAULT_ALL)  //所有的设为系统默认,一般和短信通知一样
                             .build();
         
       //设置默认   DEFAULT_LIGHTS  默认灯
         //DEFAULT_SOUND   默认声音
         //DEFAULT_VIBRATE  默认震动
         //DEFAULT_ALL 以上默认
         
         
         //led灯的显示
         notification.ledARGB = Color.RED;
         notification.ledOffMS = 0;
         notification.ledOnMS = 1;
         notification.flags = notification.flags | Notification.FLAG_SHOW_LIGHTS;
//         通常为了简便写成notification.flags |= Notification.FLAG_SHOW_LIGHTS;,这样可以为通知设置多个flag
         notification.flags |= Notification.FLAG_AUTO_CANCEL;
         
         
         //通知来时候震动,vibrate是震动的方式,
//         * final Activity activity  :调用该方法的Activity实例 
//         * long milliseconds :震动的时长,单位是毫秒 
//         * long[] pattern  :自定义震动模式 。数组中数字的含义依次是[静止时长,震动时长,静止时长,震动时长。。。]时长的单位是毫秒 
//         * boolean isRepeat : 是否反复震动,如果是true,反复震动,如果是false,只震动一次 
         long[] vibrate = new long[] { 1000, 1000, 1000, 1000};
         notification.vibrate = vibrate;
         
//         资源会获得应用的uri的方法
         //通知来时候的声音
         Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.zp1);
         notification.sound=uri;//Uri.parse("/data/data/zp1.mp3");
         //启动notification
         notificationManager.notify(NOTIFY_ID, notification); //第一个参数是标识通知的唯一码,上面自己定义的变量
    	
         
         
//  **************android 4.0前*************************************
//    	String title="mynotice";//通知栏的标题
//    	String store="hahagaga";
//    	 NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
//         Notification notification = new Notification();  
//         notification.flags |= Notification.FLAG_SHOW_LIGHTS;  
//         notification.flags |= Notification.FLAG_AUTO_CANCEL;  
//         notification.defaults = Notification.DEFAULT_ALL;  
//         notification.icon = R.drawable.hahagaga;  
//         notification.when = System.currentTimeMillis();  
//   
//         Intent intent = new Intent(this,MainActivity.class);  
//         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
//         intent.putExtra("store", store);  
//         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT  
//          //Change the name of the notification here  
//         notification.setLatestEventInfo(this, title, store, contentIntent);  
//         notificationManager.notify(NOTIFY_ID, notification);  
         
    }
    
   
    
}

  

Notification的学习,4.0前后的差别

标签:

原文地址:http://www.cnblogs.com/bokeofzp/p/4744214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!