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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_test);
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
@Override
public 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);
}
}
Android Notification用法
最新推荐文章于 2025-05-07 19:46:16 发布