android 4.0 前后很多api都有了较大的差别,不多说现在学习下notification前后实现的差别,本文参考了
:http://my.oschina.net/ososchina/blog/353692;http://gundumw100.iteye.com/blog/1873318;
http://blog.csdn.net/wukunting/article/details/5661769
先把没有注释的代码贴上,不明白的还可以看下面的带带注释的代码,希望对跟我一样的初学者有所帮助:
代码:
public class MainActivity extends ActionBarActivity {private static final int NOTIFY_ID = 0; @Overrideprotected 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); //点击notification需要激活的activity,这里设置自己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  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();//led灯的显示notification.ledARGB = Color.RED;notification.ledOffMS = 0;notification.ledOnMS = 1;notification.flags = notification.flags | Notification.FLAG_SHOW_LIGHTS;notification.flags |= Notification.FLAG_AUTO_CANCEL;long[] vibrate = new long[] { 1000, 1000, 1000, 1000};notification.vibrate = vibrate;Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.zp1);notification.sound=uri;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);  }
 
注释:
public class MainActivity extends ActionBarActivity {private static final int NOTIFY_ID = 0; @Overrideprotected 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;,这样可以为通知设置多个flagnotification.flags |= Notification.FLAG_AUTO_CANCEL;//通知来时候震动,vibrate是震动的方式,
//      long[] vibrate  :自定义震动模式 。数组中数字的含义依次是[静止时长,震动时长,静止时长,震动时长。。。]时长的单位是毫秒 long[] vibrate = new long[] { 1000, 1000, 1000, 1000};notification.vibrate = vibrate;//         设置声音
//         获取工程文件下的资源的uri方式和获取sd卡路径的方式//Uri.parse("/data/data/zp1.mp3");Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.zp1);notification.sound=uri;//启动notificationnotificationManager.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);  }}