安卓通知的使用系列2:状态栏通知和自定义状态栏通知通知

状态栏通知是android开发中常见的一种通知形式,下面我们来介绍一下它的使用方法。

整体思路:在xml文件中放置两个button控件,分别设置它们的点击事件,声明NotificationManager对象和Notification.Builder对象,在onCreate方法中实例化这两个对象,在第一个点击事件中设置通知的各个属性并发送通知;在第二个点击事件中绑定一个自定义的xml'文件并发送通知。

activity_main.xml文件:

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="135dp"
        android:text="普通的通知使用" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="47dp"
        android:text="自定义通知的使用" />
custom_notification.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:padding="10dp">
    
    <ImageView 
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="100dp"
        />
    
    <TextView 
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/image"
        />
    
    <TextView 
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/image"
        android:layout_below="@+id/title"
        />
    
</RelativeLayout>
MainActivity.java文件:

public class MainActivity extends Activity {

	private Button button, button2;
	private NotificationManager manager;
	private Notification.Builder builder;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		// 创建一个通知管理类
		manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		builder = new Notification.Builder(this);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this,
						MainActivity.class);
				// 第二个参数表示请求的状态码,
				PendingIntent contentIntent = PendingIntent.getActivity(
						MainActivity.this, 0, intent, 0);
				builder.setContentIntent(contentIntent);
				builder.setContentTitle("new notification is coming");
				builder.setContentText("hello world");
				builder.setTicker("有通知来了");// 第一次出现在状态栏的内容
				builder.setSmallIcon(R.drawable.ic_launcher);
//			         通知的形式:闪光灯、声音、震动、自己设置的声音
//				也可以定义震动的形式,在api中都有介绍
				// Notification.DEFAULT_ALL表示所有的提示都是默认的
				// DEFAULT_LIGHTS默认的闪光,DEFAULT_SOUND默认的声音,DEFAULT_VIBRATE默认的震动
				 builder.setDefaults(Notification.DEFAULT_ALL);
				// Uri uri=Uri.parse("file:///sdcard/notification/ringer.mp3");
				// builder.setSound(uri);//提示用户可以自定义设置铃声
				Notification notification = builder.build();// 仅仅限于在高版本4.1以上使用
				// notification.defaults=Notification.DEFAULT_SOUND; //在低版本中使用
				// 第一个参数表示通知的唯一标示符
				manager.notify(1000, notification);
			}
		});

		button2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				RemoteViews contentViews = new RemoteViews(getPackageName(),
						R.layout.custom_notification);
				contentViews.setImageViewResource(R.id.image,
						R.drawable.ic_launcher);
				contentViews.setTextViewText(R.id.title, "自定义通知的标题");
				contentViews.setTextViewText(R.id.text, "自定义通知的内容");

				Intent intent = new Intent(MainActivity.this,
						MainActivity.class);
				PendingIntent pendingIntent = PendingIntent.getActivity(
						MainActivity.this, 0, intent, 0);
				builder.setContentIntent(pendingIntent);
				builder.setContent(contentViews);//指定自定义布局
				Notification notification=builder.build();
				manager.notify(10001,notification);			
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值