background activity from foreground

Put your problem here if it does not fit any of the other categories.

background activity from foreground

Postby cybergatto » Sat Feb 25, 2012 5:19 pm

HI,
i need to send to foreground one activity that i've put in background by typing:
Code: Select all
moveTaskToBack(true);

This activity have one active notification so with the following code i can put the activity in foreground again.

Code: Select all
Intent notificationIntent = new Intent(this, IDontDisturbActivity.class);
        notificationIntent.setAction("android.intent.action.MAIN");
        notificationIntent.addCategory("android.intent.category.LAUNCHER");     
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        notification.flags=notification.FLAG_FOREGROUND_SERVICE;
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

But if i don't launch the main activity manually, and the activity is launched on the system startup by this code there is no way to get the activity in foreground again.

Code: Select all
public class BootUpReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
            Intent i = new Intent(context, IDontDisturbActivity.class); 
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i); 
    }
}

Any suggestion is welcome.
Lorenzo
cybergatto
Freshman
Freshman
 
Posts: 4
Joined: Sat Feb 25, 2012 5:13 pm

Top

Re: background activity from foreground

Postby Phyll » Sat Feb 25, 2012 6:10 pm

Hi CyberGatto,

This seems to be that you want to start your app on bootup. You don't need to put it in the background, just finish() it. If your broadcast receiver is registered properly, your app will start up like you want. It's not going to run through a power down anyway.

If you need to start your app up again from a notification just use a pending intent as it appears you have tried.

It should also be available as an icon on a screen to start it also.

I think your problem is trying to put it in the background. Just finish() it. The OS will take care of where and how it is kept.

Hope I'm not missing the point.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: background activity from foreground

Postby cybergatto » Sat Feb 25, 2012 7:10 pm

I need that this application always run on background for intercept the incoming call, if i finish the application the code stop to be executing.
cybergatto
Freshman
Freshman
 
Posts: 4
Joined: Sat Feb 25, 2012 5:13 pm

Re: background activity from foreground

Postby Phyll » Sat Feb 25, 2012 7:27 pm

Hi cybergatto,

But isn't there a broadcast receiver for that so that an app doesn't have to be running all the time? Can't you just listen for changes in the phone state?

Like look at this example:

http://www.krvarma.com/2010/08/detectin ... n-android/

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: background activity from foreground

Postby cybergatto » Sat Feb 25, 2012 10:02 pm

Hi,
tanks for the suggesstion, this wuold be very usefull!
I've tried to implement this in my code but the app will take too time to start when the phone is ringing.
The problem still remain.. i need that the app remain in background all the time.
Lorenzo
cybergatto
Freshman
Freshman
 
Posts: 4
Joined: Sat Feb 25, 2012 5:13 pm

Re: background activity from foreground

Postby Phyll » Sun Feb 26, 2012 5:12 am

Hi cybergatto,

So what takes it so long to start and why is this different than any other phone answering app?

Thanks,

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Re: background activity from foreground

Postby cybergatto » Sun Feb 26, 2012 8:35 am

Hi Phyll,
this is not a phone answering app, is a call filtering app, if the app is not yet startet when arrive a call the phone will begin ringing also if the incoming call have to be refused or silenced by the app.
Lorenzo
cybergatto
Freshman
Freshman
 
Posts: 4
Joined: Sat Feb 25, 2012 5:13 pm

Re: background activity from foreground

Postby Phyll » Sun Feb 26, 2012 12:05 pm

Hi cybergatto,

I think that is the job of the broadcast receiver. If it did the filtering job then the app would only have to start when there was a legitimate call.

As for the ringing, you could turn the normal ring or at least volume off until a legitimate call was determined, then if you want the user to answer it, turn on or play the ringing.

Leaving an app running all the time is bad practice, power robbing, unreliable and troublesome as you know. That is why I counsel you against it. Android is designed to use listeners and have them handle just such an event. But its your app. Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 12 guests