Notification(四)——Notification完整使用实例详解

简介: MainActivity如下: package cc.cv;import android.os.Bundle;import android.

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>


相关文章
|
API Android开发
Notification(状态栏通知)详解
Android中用于在状态栏显示通知信息的控件:Notification,相信大部分学Android都对他都很熟悉,而网上很多关于Notification的使用教程都是基于2.x的,而现在普遍的Android设备基本都在4.x以上,甚至是5.0以上的都有;他们各自的Notification都是不一样的!而本节给大家讲解的是基于4.x以上的Notification。
174 0
|
数据可视化 C# 开发工具
C#或Winform中的消息通知之系统本地通知(local toast notification)
C#应用通过 Microsoft.Toolkit.Uwp.Notifications NuGet包可以很方便的发送本地通知,适用于所有类型的应用(WPF、UWP、WinForms、控制台)
774 0
C#或Winform中的消息通知之系统本地通知(local toast notification)
|
Android开发
通知(Notification)
创建通知之前需要对android版本进行一个判断 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 1 activity_main.xml代码里仅有一个Button用于响应通知,代码不再展示
219 0
通知(Notification)
|
iOS开发
Notification的用法
Notification的用法
160 0
Notification的用法
|
Android开发
【错误记录】前台进程报错 ( Bad notification for startForeground invalid channel for service notification )
【错误记录】前台进程报错 ( Bad notification for startForeground invalid channel for service notification )
1266 0
【错误记录】前台进程报错 ( Bad notification for startForeground invalid channel for service notification )
如何启用SAP C4C OData Event Notification
如何启用SAP C4C OData Event Notification
111 0
|
API Android开发
|
Android开发 数据格式 XML