public class Text_Noti extends Activity {
private Button sendBtn, cancelBtn;
private Notification notification;
private NotificationManager nManager;
private Builder builder; // android.app.Notification.Builder;
private Intent intent;
private PendingIntent pIntent;
// Notification的标示ID
private static final int NOTIFYID_1 = 1;
private static final int NOTIFYID_2 = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_noti);
sendBtn = (Button) this.findViewById(R.id.send);
cancelBtn = (Button) this.findViewById(R.id.cancel);
nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
sendBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it = new Intent(getApplicationContext(),
OtherActivity.class);
PendingIntent pit = PendingIntent.getActivity(
getApplicationContext(), 0, it, 0);
// 2.新建一个Builder类
builder = new Notification.Builder(getApplicationContext());
// 依次设置是否打开自动消失,状态栏的通知提示信息,通知图标
// 通知内容的标题,通知内容
builder.setAutoCancel(true);
builder.setTicker("你有新的信息!");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("中奖通知");
builder.setContentText("恭喜你是第100名用户,点击抽奖...");
// 设置自定义的声音或者震动效果
// builder.setSound(Uri.parse("android.resource://com.jay.demo.notificationdemo2.res/"
// + R.raw.login));
builder.setContentIntent(pit);
// 3.为notification赋值,调用Builder.build()方法
notification = builder.build();
// 4.使用NotificationManage
nManager.notify(NOTIFYID_1, notification);
}
});
cancelBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
nManager.cancel(NOTIFYID_1);//关闭Notification
}
});
}
}
android-Notification
最新推荐文章于 2021-02-20 15:15:35 发布