白羽
2018-06-07
来源 :网络
阅读 3100
评论 0
摘要:本文将带你了解Android开发之通知功能如何实现,通知[Notification]是Android中比较有特色的功能,当某个应用程序希望给用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知实现。 使用通知的步骤1、需要一个NotificationManager来获得NotificationManager manager = (NotificationManager,希望本文对大家学Android有所帮助。
通知[Notification]是Android中比较有特色的功能,当某个应用程序希望给用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知实现。
使用通知的步骤
1、需要一个NotificationManager来获得
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
2、使用builder模式[构造函数参数太多怎么办](//www.importnew.com/6605.html)创建通知对象。
3、调用manager.notify(1, notification);
见代码:
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
xmlns:tools="//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">
<Button
android:id="@+id/send_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send notice"/>
</LinearLayout>
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频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号