android - 不能从静态上下文引用通知`notify()`
问题描述
我正在使用一个简单的代码通过一个函数在 Android 中推送通知。该功能如下:
public void sendNotification(View view) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setContentTitle("My notification").setContentText("Hello World!");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager.notify().mNotificationManager.notify(001, mBuilder.build());
}}
这是我在网站上选择的示例功能。
一切正常,NotificationCompat.builder
不返回任何错误。但是notify()
最后一行的第一个返回以下错误:Non-static method 'notify()' cannot be referenced from a static context
。
我真的不明白为什么。虚无被放置在我的MainActivity.java
内心public class MainActivity extends AppCompatActivity {}
编辑 :
解决方案是NotificationManager.notify().
从mNotificationManager.notify(001, mBuilder.build());
解决方案
请参阅下面的解决方案。
public void sendNotification(View view) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setContentTitle("My notification").setContentText("Hello World!");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(001, mBuilder.build());
}}
推荐阅读
- amazon-web-services - dynamodb 是否适合成长型或可旋转的产品?
- python - 使用请求 HTML 找不到 css 类
- python - Django 对象 order_by 给我重复的用户
- rest - Golang REST API 项目结构
- javascript - 在某些特定网站上更改 chrome 扩展程序的图标
- reactjs - 如何使用物化和 scss 防止 @media 查询错误
- javascript - JavaScript:对象键的快速随机索引
- ios - 无法编译 xcode 项目,因为缺少 info.plist
- javascript - Tensorflow.js 分词器
- javascript - Javascript - 在子块中重新定义变量 // 变量阴影