android-----通知栏(Notification)

本文介绍了一个简单的Android应用案例,展示了如何使用按钮触发通知的发送与取消,并通过代码实现了通知栏消息的具体配置。

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

前端

<?xml version="1.0" encoding="utf-8"?>
<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: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.wuzuo.notification.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_send"
        android:text="发送消息"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_cancel"
        android:text="取消消息"/>
</LinearLayout>
</RelativeLayout>

androidanifest.xlm

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wuzuo.notification">
    <span style="color:#FF0000;"><uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />获取手机震动响铃的权限</span>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
后端

package com.example.wuzuo.notification;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.renderscript.ScriptGroup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener{
private NotificationManager manager;//构建一个通知栏管理者
    private Button button1;
    private Button button2;
    int notification_ID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        button1= (Button) findViewById(R.id.button_send);
        button2= (Button) findViewById(R.id.button_cancel);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button_send:
                sendnotification();
                break;
            case R.id.button_cancel:
            manager.cancel(notification_ID);//取消通知栏
        }

    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private  void sendnotification()
    {
        Intent intent=new Intent(this,MainActivity.class);//跳转到主页
        PendingIntent pintent=PendingIntent.getActivity(this,0,intent,0);//等待未决定的
        Notification.Builder builder=new Notification.Builder(this);//创建一个Notification 下的builder工程师builder
        builder.setSmallIcon(R.mipmap.ic_launcher);//头像              /*工程师建造了头像,主题,铃声*/
        builder.setTicker("Hello");//显示在头栏里面的
        builder.setContentTitle("起床啦!");//通知栏里面的主题
        builder.setWhen(System.currentTimeMillis());//时间
        builder.setContentText("点击可在睡十分钟");//通知栏里的副主题
        builder.setDefaults(Notification.DEFAULT_ALL);//震动,响铃
        builder.setContentIntent(pintent);
        Notification notification=builder.build();//起个名字叫notification
        manager.notify(notification_ID,notification);//利用通知栏管理者的notify属性去管理notification
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值