日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Notification的学习,4.0前后的差别,和在设置声音的时候获取资源的uri方法

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Notification的学习,4.0前后的差别,和在设置声音的时候获取资源的uri方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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); }}

  

轉載于:https://www.cnblogs.com/bokeofzp/p/4744214.html

總結

以上是生活随笔為你收集整理的Notification的学习,4.0前后的差别,和在设置声音的时候获取资源的uri方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。