Android(NotificationManager 發送通知)

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   java   os   io   

該應用的介面如下,介面代碼在此不再給出

MainActivity.java

 1 public class MainActivity extends Activity { 2     private TextView tvTitle; 3     private TextView tvContent; 4     private Button btnSend; 5     private String title; 6     private String content; 7      8     public void onCreate(Bundle savedInstanceState) { 9         super.onCreate(savedInstanceState);10         setContentView(R.layout.activity_main);11         12         tvTitle=(TextView) findViewById(R.id.editText1);13         tvContent=(TextView) findViewById(R.id.EditText01);14         btnSend=(Button) findViewById(R.id.btnSend);15 16         btnSend.setOnClickListener(new OnClickListener(){17 18             @Override19             public void onClick(View v) {20                 // TODO Auto-generated method stub21                 send();22             }23         });24         25     }26     public void send(){27         title=tvTitle.getText().toString();//標題28         content=tvContent.getText().toString();//內容29         30         //1.得到NotificationManager服務31         NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);32         //2.執行個體化一個通知,指定表徵圖、概要、時間33         Notification n = new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());34         //3.指定通知的標題、內容和intent35         Intent intent = new Intent(this, MainActivity.class);36         PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0);37         n.setLatestEventInfo(this, title, content, pi);38         //指定聲音39         //n.defaults = Notification.DEFAULT_SOUND; 40         //4.發送通知41         nm.notify(1, n);42     }43 44 }

點擊“發送”按鈕,即可在通知欄顯示發送的訊息!

 

聯繫我們

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