我已在通知栏上成功创建了一个项目,其中包含以下代码,其中DownloadService扩展了IntentService类。
public class MyService extends Activity
{
...
void startService()
{
...
Intent myIntent = new Intent(StoryGallery.this,DownloadService.class);
myIntent.putExtra("story name", story.title);
startService(myIntent);
}
}
问题是如何点击通知项目以返回我的应用程序?
似乎有很多这样的问题,但我无法通过点击通知项找到关于如何启动应用的直截了当的答案。我试图让" DownloadService实现OnClickListener",但点击后没有任何响应。
所以提前感谢您的回复。
答案 0 :(得分:1)
Service
不应该实现onClickListener,因为它没有UI。它只是在后台运行。因此,只要您想显示通知,请使用:
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
/** IMPORTANT : Create an intent which starts the activity **/
Intent intent = new Intent(context, ActivityName.class);
/** IMPORTANT : Ensure that you use getActivity with the PendingIntent. **/
/**This pending intent will be fired when you click on the notification.**/
builder.setContentIntent(PendingIntent.getActivity(context, 0, intent,
0));
builder.setContentTitle(title);
builder.setContentText(message);
builder.setSmallIcon(R.drawable.notification_icon);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, notification);