android的notification的寫法

來源:互聯網
上載者:User

標籤:notification

這樣來寫:

public void onCreate() {super.onCreate();ntfmngr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);setNotifyType(R.drawable.logo,"aaaaaaaaaaa",3);}private void setNotifyType(int icon, String text, int num) {Intent notifyIntent = new Intent().setClass(getApplicationContext(),MainActivity.class);// notifyIntent.putExtra("mode", mNotifyMode);notifyIntent.setAction(Constants.UPDATE_HOME_LIST); //根據這個收到的activity可以接受這個action,判斷是什麼類型的訊息// notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |// Intent.FLAG_ACTIVITY_NEW_TASK);PendingIntent appIntent = PendingIntent.getActivity(this, 0,notifyIntent, 0);Notification myNotification = new Notification();// myNotification.defaults = Notification.DEFAULT_VIBRATE;Uri soundUri = Uri.parse("android.resource://com.sina.weibo/raw/notificationsound");// Log.e(Constants.TAG, soundUri.getPath());myNotification.flags = Notification.FLAG_AUTO_CANCEL;myNotification.icon = icon;<strong>myNotification.defaults = Notification.DEFAULT_SOUND;myNotification.sound = soundUri; //notication過來的時候有聲音</strong>if (num != -1) {myNotification.number = num;}long[] vb = new long[] { 80, 80 };myNotification.vibrate = vb;myNotification.tickerText = text;myNotification.setLatestEventInfo(this, getString(R.string.app_name),text, appIntent);System.out.println("====ntfmngr.notify");<strong>ntfmngr.notify(NOTIFY_MESSAGE, myNotification); </strong><pre name="code" class="java"><strong>//NOTIFY_MESSAGE為notification的key,可以根據key來取消</strong>
}



必須加入相應的許可權才有震動

    <uses-permission android:name="android.permission.VIBRATE" />

可以這樣來取消notication:

if (ntfmngr != null) {ntfmngr.cancelAll();}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.