From docs I understand that our program goes to background when we open another_one then after a while android will call onDestroy() to take back it's resources ; But our program is kind of watcher (imagine we have a mediaplyer which playes music in backGround) and it should not be closed until int programState == 1 (There is a power button which ends the player or watcher ) . Besides I learned that we can stay foreground with "Notification" which is so cool (But I don't know how) ! Here is a notification code:
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() ;
}
But here we have 2 problems . First I don't think notification is really attached to Activity (Maybe it needs some permissions or use service not sure !? ) ! Next I want notification to resume app not restart it !? (I think it starts activity from onCreate() ) How force it back to where we were ?
I've searched alot but still couldn't find good answers about this . Any Idea ?
To achieve this you have to use a Service . Have a look at this tutorial .
You can start the service as a "foreground service", which runs in the background and will hardly ever get killed by the system.
To do this, call startForeground(id, notification)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.