我有一个在后台运行的服务,我想通知用户是否有新数据,我已经使用了通知管理器,并且会显示通知,但点击它时它没有做任何事情(它是应该展示一项活动 我是android的新手,这是我的代码
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.shoppingcarticon,
"Nouvelle notification de ShopAlert", System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this,GooglemapActivity.class);
PendingIntent activity = PendingIntent.getService(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title",
"This is the text", activity);
notification.number += 1;
notificationmanager.notify(0, notification);
您的帮助将不胜感激。
答案 0 :(得分:4)
您可以使用此代码。将Main.Class更改为您的活动类,并根据需要将标题和内容文本更改。
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_action_search;
CharSequence tickerText = "Pet Parrot";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon,
tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Context context = getApplicationContext();
CharSequence contentTitle = "Hungry!";
CharSequence contentText = "your parrot food meter is: ";
Intent notificationIntent = new Intent(context,
Main.class);
PendingIntent contentIntent = PendingIntent
.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle,
contentText, contentIntent);
mNotificationManager.notify(1, notification);
// Log.d("test", "Saving Data to File from Service.");
答案 1 :(得分:3)
以下一行:
PendingIntent activity = PendingIntent.getService(this, 0, intent, 0);
应该是这样的:
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
因为您尝试启动Activity
,而不是Service
。希望这会有所帮助。