java如何使用通知?
从文档中我了解到,当我们打开另一个应用程序时,我们的程序会进入后台,过了一段时间,安卓会调用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()开始活动)如何强制它回到我们原来的位置
我搜索了很多,但仍然找不到关于这个的好答案。知道吗
共 (0) 个答案