手机通知功能的实现(Notifaction)

本文详细介绍了一个Android应用如何通过代码实现发送和删除自定义通知的功能,包括设置通知的样式、行为和跳转动作,以及如何在手机上正确配置通知权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意:运行完程序,需要在手机上将此软件的通知栏权限打开

在这里插入图片描述

权限:

<!--闪光灯权限-->
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    <!--振动权限-->
    <uses-permission android:name="android.permission.VIBRATE"/>

控制消息发送的界面
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <Button
        android:layout_width="209dp"
        android:layout_height="84dp"
        android:textSize="20dp"
        android:onClick="send"
        android:text="发送Notification" />

    <Button
        android:layout_width="208dp"
        android:layout_height="84dp"
        android:textSize="20dp"
        android:onClick="del"
        android:text="删除Notification" />

</LinearLayout>

点击通知消息跳转的界面
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
    tools:context=".OtherActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:text="恭喜您中了一等奖!"/>
</LinearLayout>
package com.example.test18;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    static final int NOTIFICATION_ID = 0x123;
    NotificationManager nm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取系统的Notificationmanager服务
        nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    }
    //为发送通知的按钮的点击事件定义事件处理方法
    public void send(View source){
        //创建启动其他Activity的Intent
        Intent intent = new Intent(MainActivity.this,OtherActivity.class);
        PendingIntent pi = PendingIntent.getActivity(
                MainActivity.this, 0, intent, 0);
        Notification notify = new Notification.Builder(this)
                // 设置打开该通知,该通知自动消失
                .setAutoCancel(true)
                // 设置显示在状态栏的通知提示信息
                .setTicker("有新的消息")
                // 设置通知的图标
                .setSmallIcon(R.drawable.ic_launcher_background)
                // 设置通知内容的标题
                .setContentTitle("一条新通知")
                // 设置通知内容
                .setContentText("恭喜您,您中奖了!")
                // 设置使用系统默认的声音、默认LED灯
                 .setDefaults(Notification.DEFAULT_SOUND |Notification.DEFAULT_LIGHTS)
                // 设置通知的自定义声音
//                .setSound(Uri.parse("android.resource://org.crazyit.ui/"
//                        + R.raw.msg))
                .setWhen(System.currentTimeMillis())
                // 设该通知将要启动程序的Intent
                .setContentIntent(pi)  // ①
                .build();
        // 发送通知
        nm.notify(NOTIFICATION_ID, notify);
    }
    // 为删除通知的按钮的点击事件定义事件处理方法
    public void del(View v)
    {
        // 取消通知
        nm.cancel(NOTIFICATION_ID);
    }
}

package com.example.test18;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class OtherActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值