利用service去执行通知栏消息为什么会报错

王子小沫 2014-08-14 07:35:50
利用service去执行通知栏消息为什么会报错,把service改成activity就是没问题的~~~~
可是有通知消息的时候我并不像跳activity啊 只想在通知栏显示消息!
代码如下:
①Intent service = new Intent(context, NotificationService.class);
context.startService(service);//为什么会报错,而且没有报错信息~~
②intent.setClass(context, CupalarmActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);//这个会跳到CupalarmActivity,有通知栏信息,可是我不想要页面的跳转

利用service ,显示通知栏的具体代码:为什么Service会出错 求大神给点儿意见
public class NotificationService extends Service {
/**
* 日期的年月日
*/
private int[] yearmonthday1 = new int[3];
/**
* 继承Service必须实现的方法,这里用不到
*/
public IBinder onBind(Intent intent) {
return null;
}

/**
* 初始化
*/
@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onDestroy() {

}

@Override
public void onStart(Intent intent, int startid) {
new Thread() {
public void run() {
// 定义NotificationManager
// 获取通知管理器
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int icon = android.R.drawable.stat_notify_chat;
long when = System.currentTimeMillis();
Notification n = new Notification(icon, "通知栏demo提醒", when);
n.defaults = Notification.DEFAULT_SOUND;
n.flags |= Notification.FLAG_AUTO_CANCEL;
//审核
Bundle bundle = new Bundle();
SharedPreferences preferences = getSharedPreferences("user",
Activity.MODE_PRIVATE);
String usertypeid = preferences.getString("typeid", null);

Calendar c = Calendar.getInstance();
yearmonthday1[0] = c.get(Calendar.YEAR);
yearmonthday1[1] = c.get(Calendar.MONTH);
yearmonthday1[2] = c.get(Calendar.DAY_OF_MONTH);
String nowdate = Functional.SETDATE(yearmonthday1);
// 放入审核人
bundle.putString("OperatorId", usertypeid);
// 放入时间
bundle.putString("StartDate", nowdate);
bundle.putString("EndDate", nowdate);
// 放入单据类型ID
bundle.putString("BillTypeID", "");
// 放入单据编号
bundle.putString("BillCode", "");
// 放入备注
bundle.putString("Comment", "");
// 放入审核状态
bundle.putString("IfAudit", "0");
// 放入页数==0
bundle.putString("CountNub", "1");
Intent NotiIntent = new Intent((Activity) MyActivity.getContext(), Shenhecaogao1.class);
PendingIntent pi = PendingIntent.getActivity(
(Activity) MyActivity.getContext(), 0,
NotiIntent.putExtras(bundle),
PendingIntent.FLAG_CANCEL_CURRENT);
n.setLatestEventInfo((Activity) MyActivity.getContext(),
"通知栏demo提醒title", "通知栏demo提醒text", pi);
nm.notify(0, n);

};
}.start();

}
}
...全文
190 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
王子小沫 2014-08-25
  • 打赏
  • 举报
回复
引用 2 楼 u014023033 的回复:
没有报错信息 ~~~ 奇怪
是的 第一次遇到这种状况
王子小沫 2014-08-25
  • 打赏
  • 举报
回复
引用 3 楼 CjiajiaILOVEU 的回复:
报错,没有报错信息?
可以的 后来我重新用了中方法解决了 这个方法 确实没有报错信息 第一次见到的状况
王子小沫 2014-08-25
  • 打赏
  • 举报
回复
引用 5 楼 lionfresh 的回复:
你的服务注册了吗?
注册了
lionfresh 2014-08-19
  • 打赏
  • 举报
回复
你的服务注册了吗?
杰哥哥啊 2014-08-19
  • 打赏
  • 举报
回复
logcat中没有log? 话说你的那个MyActivity是从哪里来的,在service中可以直接引用到这个吗
飘荡的心 2014-08-18
  • 打赏
  • 举报
回复
报错,没有报错信息?
王子小沫 2014-08-18
  • 打赏
  • 举报
回复
没有报错信息 ~~~ 奇怪
ghostgoodbye 2014-08-15
  • 打赏
  • 举报
回复
把报错信息给贴出来吧!

80,337

社区成员

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

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