Android Notification狀態列通知

來源:互聯網
上載者:User

標籤:his   rip   pretty   long   app   set   xtend   lin   bundle   

沒有添加額外的震動及聲音效果,這裡直接實現了通知的功能,看效果吧:MainActivity.java
package com.example.notification;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {public NotificationManager mNotificationManager;@SuppressLint("NewApi")@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn = (Button) findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 1-獲得MotificationManager的引用。String ns = Context.NOTIFICATION_SERVICE;mNotificationManager = (NotificationManager) getSystemService(ns);// 2-執行個體化Notification:int icon = R.drawable.new_mail;CharSequence tickerText = "Hello";long when = System.currentTimeMillis();Notification notification = new Notification(icon, tickerText,when);// 3-定義Notification,如顯示icon、目標intent等資訊Context context = getApplicationContext();CharSequence contentTitle = "My notification";CharSequence contentText = "Hello World!";Intent notificationIntent = new Intent(v.getContext(),MessageActivity.class);PendingIntent contentIntent = PendingIntent.getActivity(v.getContext(), 0, notificationIntent, 0);notification.setLatestEventInfo(context, contentTitle,contentText, contentIntent);// 4-傳遞給Manager.final int HELLO_ID = 1;mNotificationManager.notify(HELLO_ID, notification);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}
MessageActivity.java
package com.example.notification;import android.app.Activity;import android.app.NotificationManager;import android.content.Context;import android.os.Bundle;public class MessageActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_message);NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);mNotificationManager.cancel(1);//這裡實現了,點擊訊息後,自己主動清除訊息的功能。}}
XML布局檔案就不寫了。非常easy的~這裡沒有實現通知到達時的提示效果,如震動、提示音之類。將在 Android Notification實現推送訊息過程中接受到訊息端有聲音及震動及亮屏提示 文章實現這樣的功能

Android Notification狀態列通知

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.