Notification基础Demo
主类:
package com.amaker.notification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.provider.Contacts; import android.view.View; import android.widget.Button; /** * 通知基础学习 * 如何定义一个通知(通知的一些属性) * 如何取消通知 * ZZL */ public class MainActivity extends Activity implements android.view.View.OnClickListener { private Button notification_btn; private Button cancel_btn; private NotificationManager nm; private Notification n; public static final int ID = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); notification_btn = (Button) findViewById(R.id.notification_button1); cancel_btn = (Button) findViewById(R.id.cancel_button2); notification_btn.setOnClickListener(this); cancel_btn.setOnClickListener(this); } //发 出一个通知 void noti(){ //实例化通知管理器 nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //实例化一个通知 n = new Notification(); //通知上显示的那张小图标 n.icon = R.drawable.icon; //通知的tickerText,显示一下就消失了 n.tickerText = "tickerText......"; //通知发出时间 n.when = System.currentTimeMillis(); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0); n.setLatestEventInfo(this, "contentTitle", "contentText", contentIntent); //通知封装好了后,通过通知管理器把通知发送出去 nm.notify(ID, n); } @Override public void onClick(View v) { switch (v.getId()) { //点击按钮,发出一个通知 case R.id.notification_button1: noti(); break; //点击按钮取消通知 case R.id.cancel_button2: nm.cancel(ID); break; default: break; } } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="Notification" android:id="@+id/notification_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="Cancel" android:id="@+id/cancel_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28