MainActivity如下:
package cc.cv; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; /** * Demo描述: * Notification完整使用实例 * * 注意权限: * <uses-permission android:name="android.permission.VIBRATE"/>" * * * 参考资料: * 1 http://blog.csdn.net/vipzjyno1/article/details/25248021 * 2 http://adchs.github.io * Thank you very much */ public class MainActivity extends Activity { private Button mSendButton; private Button mUpdateButton; private Context mContext; private Builder mBuilder; private PendingIntent mPendingIntent; private Notification mNotification; private NotificationManager mNotificationManager; private final int NOTIFICATION_ID=9527; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mContext=this; mSendButton=(Button) findViewById(R.id.sendButton); mSendButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { sendNotification(mContext); } }); mUpdateButton=(Button) findViewById(R.id.updateButton); mUpdateButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { updateNotification(); } }); } /** * 发送通知 */ private void sendNotification(Context context){ Intent intent=new Intent(mContext, MainActivity.class); mPendingIntent=PendingIntent.getActivity(mContext, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT); mNotificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mBuilder=new Builder(mContext); //通知产生的时间 mBuilder.setWhen(System.currentTimeMillis()); //通知首次出现在通知栏时的提示文字 mBuilder.setTicker("Ticker"); mBuilder.setContentTitle("ContentTitle"); mBuilder.setContentInfo("ContentInfo"); mBuilder.setContentText("ContentText"); mBuilder.setContentIntent(mPendingIntent); //通知的优先级 mBuilder.setPriority(Notification.PRIORITY_DEFAULT); //设置通知的图标.必须要有这句否则通知不显示!!! mBuilder.setSmallIcon(R.drawable.ic_launcher); //为通知添加声音,闪灯和振动效果等效果 mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); //设置通知是否为一个正在进行的通知.后台任务时常使用true mBuilder.setOngoing(false); mNotification=mBuilder.build(); //通知被点击后自动消失 mNotification.flags = Notification.FLAG_AUTO_CANCEL; mNotificationManager.notify(NOTIFICATION_ID, mNotification); } /** * 发送通知 */ private void updateNotification(){ mBuilder.setContentTitle("Title"); mBuilder.setContentInfo("Info"); mBuilder.setContentText("Text"); mNotification=mBuilder.build(); mNotification.flags = Notification.FLAG_AUTO_CANCEL; mNotificationManager.notify(NOTIFICATION_ID, mNotification); } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/sendButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送通知" android:layout_centerHorizontal="true" android:layout_marginTop="100dip" /> <Button android:id="@+id/updateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="更新通知" android:layout_centerHorizontal="true" android:layout_marginTop="250dip" /> </RelativeLayout>
时间: 2024-10-28 13:56:05