android学习之六·是用系统nofity实现系统消息推送



/bywinkey 整理时间:20151722:26:44

前言:本来这一节要学习继承TabActivity实现TabHost的,我找了大量的资料,都没有实现出来,估计本人学艺不精,小伙伴们如果有的话可以分享出来哦。

  1. 概述:当我们收到短信息之后,系统通知栏会以滚动的方式显示出来、或者在下载了某个app之后,例如暴风影音,那么当手机连接上wifi之后,暴风影音就会在系统通知栏提示最新上映的影视剧,或者新闻信息,那么, android系统提供的Notification组件就可以完成这些功能了。

  2. 预备知识:

    1. Notification是系统组件,切系统中只有一个,因此该组件通过调用Context.getSystemServer(NOTIFICATION_SERVER)方法即可返回NotificationManager实例,这是一个管理Notification的服务,Notification需要实例化之后才可以使用,其中构造方法为:
      notification = new Notification(R.drawable.red,"
      推广信息",System.currentTimeMillis());
      第一个参数为推送要设置的图标,第二个参数为标题,第三个是系统时间

    2. 推广的信息要转到别的地方去,需要使用Intent意图进行转向,Notification中使用PendingIntent 封装的Intent转到这个消息需要连接的地方,PendingIntent的使用方法:PendingIntent peding = PendingIntent.getActivity(Content,0,Intent,0);

    3. 消息推送时,还可以使用系统的震动功能,以及播放铃声,但这些操作都需要在系统AndroidManifest.xml中进行设定权限(使用方法在文档结尾说明)

  3. 步骤:

    1. 新建一个Model

    2. layout.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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button
            android:id="@+id/btn_send"
            android:text="发送通知"
            android:textSize="20sp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btn_clear"
            android:text="取消通知"
            android:textSize="20sp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        </LinearLayout>

</RelativeLayout>


这里只要放两个按钮就行,需要点击这两个按钮进行发送消息和清除消息的操作

                   c).Activity.java中添加以下代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获得btn
        sendBtn = (Button) findViewById(R.id.btn_send);
        cancleBtn = (Button) findViewById(R.id.btn_clear);
        //获得系统服务管理器
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//获取到系统的服务
        //通过构造器创建一个Notification对象,并且设置图标
        notification = new Notification(R.drawable.red,"推广信息",System.currentTimeMillis());
        //给notification设置属性
        notification.defaults=Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;//默认声音
        notification.flags = Notification.FLAG_AUTO_CANCEL;//可以被通知栏清除掉
        //定义一个意图转到别的地方、
        Intent openIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://cn.msn.com/?ocid=iehp&pc=EUPP_EPHPC"));
        //是用 PendingIntent 当用户单击消息时,会启动该意图(Intent)
        PendingIntent contentIntent = PendingIntent.getActivity(this,0,openIntent,0);
        //将PendingIntent放入
        notification.setLatestEventInfo(this,"MSN中文网","Microsoft中文网,微软国内唯一的门户网站,这里有游戏,新闻,wp最新的app以及microsoft最新的包括软件在内的最新消息",contentIntent);
        //添加按钮事件
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNotificationManager.notify(0,notification);
            }
        });
        cancleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNotificationManager.cancel(0);
            }
        });
}


运行结果如下图所示:



4.关于Notification中的一些相关设定:


         4.1Notification常用设定:


         在上面提到过,消息被推广时会出现铃声提示以及震动, 下表中列出常用功能(代码中添加了默认铃声和震动功能)


        


属性值

 

Notification.DEFAULT_ALL

默认声音,震动(),闪光灯()

Notification.DEFAULT_LIGHTS

使用默认闪光灯()

Notification.DEFAULT_SOUND

使用默认声音

Notification.DEFUALT_VIBRATE

默认手机震动()


  1. :需要添加震动权限

  2. :需要添加闪光灯权限


添加权限方法:<uses-permissionandroid:name="android.permission.VIBRATE"/> (震动)


<uses-permissionandroid:name="android.permission.FLASHLIGHT"/>


上述提到都是默认的,其实可以声音闪光灯和震动都是可以进行自定义的


例如:设定声音的方法:


Notify.sound=Uri.parse(“声音文件的路径”);


Notify.ledOffMs=1000;//设置闪光灯1000毫秒之后熄灭


Notifi.ledOnMs=1000;//设置闪光灯1000之后开启

注意:这里闪光灯不是摄像头的那个灯(我调试了好久,后来拿小米的手机试了下, 原来是前面那个呼吸灯。)




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值