关于安卓通知Notification的使用,不能现实出来,请求解决,谢谢!

AXDii 2017-10-31 10:26:50
使用的是Andriod API 26,Android Studio,在模拟器安卓8.0的手机上运行

跟着郭霖大神的《第一行代码》中8.2.1的通知的基本用法来写的

只有一个main_layout.xml,和MainActivity是我修改过代码的

想弄出点击一下按钮后在通知栏出现通知

出现的问题是:点击了按钮后,在通知栏没有出现通知

main_layout.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/send_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send notice"/>
</LinearLayout>


MainActivity如下所示:


package com.example.notificationtest;

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.util.Log;
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 test")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
manager.notify(1, notification);
Log.d("MainActivity", "test");
break;
default:
break;
}
}
}



点击按钮后,没有相应的通知,小白找不到解决方法,请求大神解答,谢谢!
...全文
1096 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
AXDii 2020-09-29
  • 打赏
  • 举报
回复
老帖子,莫回,离开安卓一线已多年
忘不川 2020-09-09
  • 打赏
  • 举报
回复
vivo/oppo? 给了通知权限了吗
立青_ 中级 2020-09-09
  • 打赏
  • 举报
回复
Android O以上需要添加通知渠道
qq_46056214 2020-09-09
  • 打赏
  • 举报
回复
楼上正解 , 要么弹出申请权限的框,要么自己手动给app添加权限
qq_39931174 2018-07-23
  • 打赏
  • 举报
回复
8.0需要使用消息通道 https://blog.csdn.net/guolin_blog/article/details/79854070
Jing丶無雙 2017-11-01
  • 打赏
  • 举报
回复
你好,我使用的是eclipse,在真机Android 5.1.1以及Android 7.0上测试了你提供的代码,通知栏是可以展现这个通知的。你可以用真机试下,另外你也得找下模拟器是否禁止了你的通知栏消息提示的权限
P_king 2017-11-01
  • 打赏
  • 举报
回复
你检查一下,是通知栏权限没有吧?

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧