我有一个Android服务,我正在使用startForeground,并且没有得到我期望得到的东西。 我使用以下代码:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("TITLE").setContentText("CONTENT").setContentInfo("INFO")
.setWhen(System.currentTimeMillis()).setAutoCancel(false)
.setContentIntent(pendIntent);
Notification notice = builder.build();
startForeground(startForegroundId, notice);
我在图标旁边看到的是:MyApp正在运行 - 到目前为止一直很好,但是我得到标准文字:“触摸以获取更多信息或停止......” 并且 - 当用户点击图标时,应用信息标准对话框。
如何更改文字? 如何在用户点击图标时运行我的MainActivity?
答案 0 :(得分:1)
在服务的onStartCommand()方法中写下以下代码:
Notification note = new Notification(R.drawable.ic_launcher,
"Foreground Service notification?", System.currentTimeMillis());
Intent i = new Intent(this, CurrentActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
Date dateService=new Date(System.currentTimeMillis());
String dateString=dateService.toString().split(" ")[1]+" "+dateService.toString().split(" ")[2]+" "+dateService.toString().split(" ")[3];
note.setLatestEventInfo(this, "Foreground service",
"Now foreground service running: "+dateString, pi);
note.flags |= Notification.FLAG_AUTO_CANCEL;
startForeground(2337, note);