利用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();
}
}