Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android服务中使用NotificationManager_Android_Service_Notificationmanager - Fatal编程技术网

在android服务中使用NotificationManager

在android服务中使用NotificationManager,android,service,notificationmanager,Android,Service,Notificationmanager,我有一个在后台运行的服务,我想通知用户是否有新数据,我使用了notificationmanager,通知会出现,但当单击它时,它不会做任何事情(它应该显示一个活动) 我是android新手,这是我的代码 NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = newNotification(

我有一个在后台运行的服务,我想通知用户是否有新数据,我使用了notificationmanager,通知会出现,但当单击它时,它不会做任何事情(它应该显示一个活动) 我是android新手,这是我的代码

NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = newNotification(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);
我们将非常感谢您的帮助。

以下几行:

PendingIntent activity = PendingIntent.getService(this, 0, intent, 0);
应该这样看:

PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);

由于您尝试启动的是
活动,而不是
服务
。希望这能有所帮助。

您可以使用此代码。将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.");

谢谢您的帮助,但它也不起作用,通知文本不会显示(在正常状态下,当我单击时,通知栏中有我的通知,但此处通知栏为空)@Yasmine GreenApple,确保代码确实执行。顺便说一下,您使用的通知初始化模式已被弃用,请查看Notification.Builder类,因为它是现在构造通知的首选方法。也不起作用,谢谢您的帮助,但您确定此代码甚至可以用于服务吗?是的这很有效,因为我是从我的服务中复制的。让我给你完整的代码,你可以随意使用它。编辑我的回答我如何发送带有消息编号的通知,这是徽章计数?