Android Notification用法

来源:互联网 发布:最好股指期货交易软件 编辑:程序博客网 时间:2024/05/19 02:27
package com.example.notificationtest;import android.os.Bundle;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.view.Menu;import android.view.View;public class NotificationTestActivity extends Activity {static final int NOTIFICATION_ID = 0x123;NotificationManager nm;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_notification_test);nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.notification_test, menu);return true;}public void send(View Source){Intent intent = new Intent(NotificationTestActivity.this,OtherActivity.class);PendingIntent pi = PendingIntent.getActivity(NotificationTestActivity.this, 0, intent, 0);Notification notify = new Notification.Builder(this).setAutoCancel(true).setTicker("有新消息").setSmallIcon(R.drawable.ic_launcher).setContentTitle("一条新通知").setContentText("恭喜你,您加薪了!").setWhen(System.currentTimeMillis()).setContentIntent(pi).build();nm.notify(NOTIFICATION_ID,notify);}public void del(View v){nm.cancel(NOTIFICATION_ID);}}

0 0