NotificationManager在service中调用不起来

对你爱爱爱不完 2016-11-19 04:43:37
我这是在service中调用NotificationManager,但在 NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 这句话的时候出错了,我吧service设置了一直运行,我是想程序关闭后,service能接收到mqtt消息,并且调用 NotificationManager显示通知,,现在在调用 NotificationManager时出错了,而且是程序打开的情况下,怎么解决? 代码如下,,






package org.xyz.mqtt;

import org.xyz.model.MyApplication;

import com.example.ftlink.MainActivity;
import com.example.ftlink.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class MqttService extends Service {
MyApplication app = null;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("service start");
// context = getApplicationContext();
app = (MyApplication) getApplication();
MqttThread sub = new MqttThread(app.getBroker(), app.getClientId(), app.getSubTopic());
app.setSub(sub);
sub.start();

}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}

//创建通知
public void CreateInform() {
/**
* 添加一个notification
*/
Log.v("test1", "真的进来了");
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Log.v("test1", "1");
// 创建一个Notification
Notification notification = new Notification();
// 设置显示在手机最上边的状态栏的图标
notification.icon = R.drawable.icon_fengshang;
Log.v("test1", "2");
// 当当前的notification被放到状态栏上的时候,提示内容
notification.tickerText = "注意了,我被扔到状态栏了" ;
Log.v("test1", "3");
/***
* notification.vibrate 当手机震动时,震动周期设置
*/
// 添加声音提示
notification.defaults=Notification.DEFAULT_SOUND;
Log.v("test1", "4");
// audioStreamType的值必须AudioManager中的值,代表着响铃的模式
notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;
Log.v("test1", "5");
//下边的两个方式可以添加音乐
Intent intent = new Intent( this , MainActivity. class );
Log.v("test1", "6");
PendingIntent pendingIntent = PendingIntent.getActivity( this , 0 , intent, PendingIntent.FLAG_ONE_SHOT);
Log.v("test1", "7");
// 点击状态栏的图标出现的提示信息设置
notification.setLatestEventInfo( this , "内容提示:" , "我就是一个测试文件" , pendingIntent);
Log.v("test1", "8");
manager.notify( 1 , notification);
}

}
...全文
191 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
跪求各位大神
Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1 安装AndroidSDK与ADTplug-in 2.2 建立第一个Android项目(HelloAndroid!) 2.3 Android应用程序架构——从此开始 2.4 可视化的界面开发工具 2.5 部署应用程序到Android手机 第3章 用户人机界面 3.1 更改与显示文字标签——TextView标签的使用 3.2 更改手机窗口画面底色——drawable定义颜色常数的方法 3.3 更改TextView文字颜色——引用Drawable颜色常数及背景色 3.4 置换TextView文字——CharSequence数据类型与ResourceID应用 3.5 取得手机屏幕大小——DisplayMetrics取得画面宽高的方法 3.6 样式化的定型对象——Style样式的定义 3.7 简易的按钮事件——Button事件处理 3.8 手机页面的转换——setContentView的应用 3.9 调用另一个Activity——Intent对象的使用 3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个Activity——startActivityForResult方法 3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView的交互 3.14 控制不同的文字字体——Typeface对象使用 3.15 如iPhone拖动相片特效——Gallery画廊 3.16 自制计算器——多按钮的整合应用 3.17 关于(About)程序信息——Menu功能菜单程序设计 3.18 程序加载,请稍后——ProgressDialog与线程整合应用 3.19 全屏幕以按钮覆盖——动态产生按钮并最大化 3.20 今晚到哪儿打牙祭?——具选择功能的对话框 3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast对象的使用 4.4 我同意条款——CheckBox的isChecked属性 4.5 消费券采购列表——多选项CheckBox的应用 4.6 向左或向右——RadioGroup组与onCheckedChanged事件 4.7 专业相框设计——ImageView的堆栈应用 4.8 自定义下拉菜单模式——Spinner与setDropDownViewResource 4.9 动态添加/删除的Spinner菜单——ArrayList与Widget的依赖性 4.10 心爱小宝贝相片集——Gallery与衍生BaseAdapter容器 4.11 快速的搜索手机文件引擎——JavaI/O的应用 4.12 按钮也能随点击变换——ImageButton选择特效 4.13 具自动提示功能的菜单——AutoCompleteTextView与数组 4.14 数字及模拟小时钟设计——AnalogClock与DigitalClock的原理 4.15 动态输入日期与时间——DatePicker与TimePicker应用 4.16 猜猜红心A在那儿——ImageView点击事件与透明度处理 4.17 后台程序运行进度提示——ProgressBar与Handler的整合应用 4.18 动态文字排版——GridView与ArrayAdapter设计 4.19 在Activity里显示列表列表——ListView的布局 4.20 以动态列表配置选项——ListActivity与Menu整合技巧 4.21 查找程序根目录下所有文件——JavaI/O与ListActivity的结合.. 4.22 加载手机磁盘里的图文件——使用decodeFile方法 4.23 动态放大缩小ImageView里的图片——运用Matrix对象来缩放图文件 4.24 动态旋转图片——Bitmap与Matrix旋转ImageView 4.25 猜猜我在想什么——RadioButtonID 4.26 离开与关闭程序的弹出窗口——对话窗口上的ICON图标 第5章 交互式通信服务与手机控制 5.1 具有正则表达式的TextView——Linkify规则 5.2 ACTION!CALL!拨打电话——Intent

80,355

社区成员

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

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