《第一行代码》第八章 通知的使用(NotificationTest)

参考:第一行代码
第八章讲的是手机多媒体的应用。虽然很多人使用手机,但是大家并不知道为什么自带有很多程序。比如自带的多媒体应用。然后我们就可以使用这些多媒体编写更多,更高质量的应用!
先上一个notification.
无论是在哪里创建通知,整体的步骤大致是相同的!
第一步先要拿到一个NotificationManager对通知进行管理,它可以通过Context的getSystemService(a)方法拿到,a为系统的服务参数。
第二步创建一个Notification对象,这个对象可以通过有参构造方法进行创建,使用有三个参数的构造方法(a,b,c)
a为通知的图标,
b为通知的ticker内容,会一闪而过,属于瞬时的提示信息。
c为通知被创建的时间。
第三步对通知的布局进行设定。使用Notification的setLatesEventInfo(a,b,c,d)就可以给通知设定一个标准的布局。
a,是Context
b,是通知标题
c为通知内容
d为pendingIntent.(要是只是通知一下,不给通知活动的话基本没有用,就传入null就可以了)
第四步,用NotificationManager对象的notify(a,b)方法让通知显示出来!
a为通知的id
b为Notification对象。
初步打完收工。

当然在里面加入了一个通知的响应活动,
PendingIntent相当于延迟的Intent,是一个类。主要提供几个静态方法用于获取实例,getActivity(),getBroadcast(),getService().
接收参数都是相同的;
a为Context;
b一般用不到,传入0;
c为Intent对象
d用于确定PendingIntent的行为
(注意通知相应活动的注册)
java:

package com.example.notifitiontest;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
    private Button sendNotice;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendNotice=(Button)findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
        switch(v.getId()){
        case R.id.send_notice:
            NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);//第一步
            Notification notification=new Notification(R.drawable.ic_launcher,"This is ticker text",System.currentTimeMillis());//第二步
            notification.defaults=Notification.DEFAULT_ALL;
            Intent intent=new Intent(this,NotificationActivity.class);
            PendingIntent pi=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
            notification.setLatestEventInfo(this, "我是你爸", "This is content text", pi);//第三步
            manager.notify(2,notification);//第四步

            break;
        default:
            break;
        }
    }

}

主活动布局:

<LinearLayout 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"
    android:orientation="vertical">  

    <Button 
        android:id="@+id/send_notice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send notice"
        />

</LinearLayout>

通知活动布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:text="This is notification layout"
        />

</LinearLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值