安卓多媒体应用-通知

22 篇文章 0 订阅
1 篇文章 0 订阅

介绍:通知(Notification)是安卓系统中比较有特色的一个功能,当某一个应用程序希望向用户发送一些提示信息,而该应用程序又不在前台运行的时,就可以借助通知来实现。发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏可以看到通知的详细内容。

1)通知的基本用法

创建通知步骤:1、需要一个NotificationManager对通知进行管理,调用Context的getSystemService()方法获取到。

              2、getSystemService()方法接收一个字符串参数用于确定获取系统哪个服务,传入Context.NOTIFICATION_SERVICE即可。

              3、实例:NotificationManager manager =(NotificatrionManager)getSystemService(Context.NOTIFICATION_SERVICE);

              4、使用Builder构造器来创建一个空的Notification对象

               Notification notification = new NotificationCompat.Builder(context).build();//Notification需要导入API,support-v4提供了一个Notification类

              5、在最终的build()方法之前可以连缀任意多的设置方法来创建一个丰富的Notification对象如下所示:

                   Notification notification = new NotificationCompat.Builder(this)

                        .setContentTitle("This is content title")//指定标题内容

                        .setContentText("This is content text")//指定通知的正文内容

                        .setWhen(System.currentTimeMillis())//通知创建时间

                        .setSmallIcon(R.drawable.big_image)//设置通知的小图标

                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),

                                R.drawable.big_image))//设置通知大图标

                        .build();

              6、最后调用NotificationManager的notify()方法就可以让通知显示出来

                    manager.notify(1, notification);//其中第一个参数是id(要保证每一个通知所制定的id都不同),第二个参数是Notification对象。

说了那么多,接下来进行实战训练一下:首先新建一个NotificationTest项目,修改activity_main.xml中的代码,

功能:添加一个Send Notice按钮,用来发出一条通知

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/sendNotice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送消息" />
</LinearLayout>

接着修改MainActivity中的代码,

 public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = (Button) findViewById(R.id.sendNotice);
        sendNotice.setOnClickListener(this);
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sendNotice:
                
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(this)
                        .setContentTitle("This is content title")
                        .setContentText("This is content text")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.drawable.big_image)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                                R.drawable.big_image))
                        .build();//显示一张大图片
                
manager.notify(1, notification);
                break;
            default:
                break;
        }

    }
}

2)通知进阶

学完了通知的基本用法之后,我们发现发出的这条通知不可以点击!所以下一步我们来完善它!要想实现通知的点击效果,需要在代码中进行相应的设置,就要用到PendingIntent。

PendingIntent跟Intent很类似他们都可以用于启动活动、启动服务以及发送广播等。而PendingIntent倾向于在某个时机去执行某个动作 。所以可以将PendingIntent简单理解为延迟执行的Intent.

PendingIntent的用法:PendingIntent提供了几个静态方法用于获取PendingIntent的实例,可以根据需求来选择是使用getActivity()方法、getBroadcast()方法,还是getService()方法。要注意的是这几个方法所接受的参数都是一样的,第一个参数是Context,第二个参数一般用不到传入0,第三个参数是Intent对象,我们可以通过这个对象构建出PendingIntent的“意图”。第四个参数用于确定PendingIntent的行为,行为有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT这四种值可选,通常情况这个参数传入0.

NotificationCompat.Builder这个构造器还可以连缀一个setContentIntent()方法,接受的参数是一个PendingIntent对象。因此可以通过PendingIntent构建出一个延迟执行的“意图”,当用户点击这条通知时就会执行相应的逻辑。

说到这相信对PendingIntent有一定了解,首先在新建一个活动 NotificationActivity,布局起名为notification_layout.然后修改布局文件代码

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

</RelativeLayout>

接着开始修改MainActivity的代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = (Button) findViewById(R.id.sendNotice);
        sendNotice.setOnClickListener(this);
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sendNotice:
                Intent intent = new Intent(this,NotificationActivity.class);//创建视图
                
PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);//获得PendingIntent 对象
                
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(this)
                        .setContentTitle("This is content title")
                        .setContentText("This is content text")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.drawable.big_image)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                                R.drawable.big_image))
                        .setContentIntent(pi)//传入PendingIntent对象
                       // .setAutoCancel(true)//第一种写法:当点击了这个通知的时候,通知会自动取消
                      //  .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/luna.ogg")))//允许播放音频
                        
.setVibrate(new long[] {0,1000,1000,1000})//手机震动一秒,然后禁止一秒
                        
.setLights(Color.GREEN,1000,1000)//实现LED灯一绿色灯光一闪一闪的效果
                       // .setDefaults(NotificationCompat.DEFAULT_ALL)//使用通知默认效果
                      /*  .setStyle(new NotificationCompat.BigTextStyle().bigText("大家好大家好大家好大家好大家好大家好大家好大家好大家好大" +
                                "家好大家好学习安卓,安卓入门与精通"))//显示所有的文字内容*/
                       // .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.big_image)))
                        
.setPriority(NotificationCompat.PRIORITY_MAX)//通知重要程度 MAX表示最高的重要程度
                        
.build();//显示一张大图片
                
manager.notify(1, notification);
                break;
            default:
                break;
        }

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值