【发布时间】:2014-10-01 10:34:30
【问题描述】:
从文档中我了解到,当我们打开 another_one 时,我们的程序会进入后台,然后过一段时间,android 会调用 onDestroy() 来收回它的资源;但是我们的程序是一种观察者(假设我们有一个在后台播放音乐的 mediaplyer),它不应该在 int programState == 1 之前关闭(有一个电源按钮可以结束播放器或观察者)。此外,我了解到我们可以通过“通知”保持前台,这非常酷(但我不知道如何)!这是一个通知代码:
in Activity's onCreate(){
//> reading some data and define some initial values .
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification app_notfiy =addNotification();
//> still should perform somehing ?!?
}
private Notification addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_new)
.setContentTitle(getText(string.app_name))
.setContentText("> This is a notification !?")
.setUsesChronometer(true)
;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
return builder.build() ;
}
但是这里我们有两个问题。首先,我不认为通知真的附加到活动(也许它需要一些权限或使用服务不确定!?)!接下来我希望通知恢复应用程序而不是重新启动它!? (我认为它从 onCreate() 开始活动)如何强制它回到我们原来的位置?
我搜索了很多,但仍然找不到关于这个的好答案。任何想法 ?
【问题讨论】:
-
我不完全确定您在这里的用例是什么,但听起来您正在寻找服务。 thorbek.net/online/2013/10/16/mediaplayer-in-background
-
@TomislavNovoselec:很好的例子。它可能会解决我的问题。坦克。
标签: java android service notifications foreground