android 中附近的功能怎么实现,Android开发之通知功能如何实现

通知[Notification]是Android中比较有特色的功能,当某个应用程序希望给用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知实现。

使用通知的步骤

1、需要一个NotificationManager来获得

NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

2、使用builder模式[构造函数参数太多怎么办](http://www.importnew.com/6605.html)创建通知对象。

3、调用manager.notify(1, notification);

见代码:

布局文件<?xml  version="1.0" encoding="utf-8"?>

xmlns:tools="http://schemas.android.com/tools"

android:orientation="vertical"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.hms.notificationtest.MainActivity">

android:id="@+id/send_notice"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Send notice"/>

MainActivity

import android.app.Notification;import android.app.NotificationManager;import android.graphics.BitmapFactory;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.app.NotificationCompat;import android.view.View;import android.widget.Button;

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.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 NotificationCompat.Builder(this)

.setContentTitle("This is content title")

.setContentText("This is content text")

.setWhen(System.currentTimeMillis())

.setSmallIcon(R.mipmap.ic_launcher)

.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))

.build();

manager.notify(1, notification);

break;

default:

}

}

}

这个通知是不能响应点击事件的,这个时候就用到了

PendingIntent

Intent更加倾向于立即去执行某个动作

而PendingIntent更加倾向于在某个合适的时机去执行某个动作

所以,也可以简单的把PendingIntent理解为延迟执行的Intent

新建一个NotificationActivity

然后改动如下

...case R.id.send_notice:

Intent intent = new Intent(this, NotificationActivity.class);

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

...

...

.setContentIntent(pi)

...

.build()

点击通知后,图标不会自动消失,可以通过

...

.setAutocancle()

...

.build();

或者在NotificationActivity中用

NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

manager.cancle(1);//这里的1是id。也就是nofify(1, notificatiion)中的1

通知还可以在点击的时候设置音频、振动、LED灯等更多功能,都可以通过

setXXX();

实现。

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值