how to close a browser programatically

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

how to close a browser programatically

Postby shailaja » Fri Mar 16, 2012 7:39 am

Hi all,
I have a requirement to close the default browser which is opened by my application using following code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent browserIntent = new Intent(Intent.ACTION_VIEW);
  2.                 browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
  3.                 browserIntent.setData(Uri);
  4.                 browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
  5.                 startActivity(browserIntent);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

And now I want to close that browser after some time. Is there a chance to close browser from my application? if any one have an idea please help me

Thanks
Shailaja
shailaja
Developer
Developer
 
Posts: 40
Joined: Fri Aug 06, 2010 8:00 am

Top

Re: how to close a browser programatically

Postby Phyll » Fri Mar 16, 2012 2:59 pm

Hi shailaja,

You can get a list of the running processes from the ActivityManager. With this you could find whether or not the browser app is running.

If you started a timer on a background thread when you start the browser, it could check with the activitymanager to see if that app was still running after a given time. If it was, it could end the browser process. That should return control to your app.

Of course a lot of things could go wrong with such a simple approach. But it could fundamentally work. You might also want to set an alarm instead.

Hope this helps.

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

Re: how to close a browser programatically

Postby shailaja » Mon Mar 19, 2012 7:34 am

Thanks Phyll,
I opened browser from my activity using browser intent. And what i want to do now is after some time close the browser. As u told by using Activity manager, Am able to get the recent processes list and tasks list by using the following code.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  ActivityManager am = (ActivityManager)getApplicationContext().getSystemService(Context. ACTIVITY_SERVICE);
  2.         List<ActivityManager.RecentTaskInfo> recentTasks = am.getRecentTasks(5, 0);
  3.          List<ResolveInfo> riList;
  4.         PackageManager pm = getApplicationContext().getPackageManager();
  5.                 riList = new ArrayList<ResolveInfo>(10);
  6.                
  7.                
  8.         for (ActivityManager.RecentTaskInfo task : recentTasks)
  9.                 {
  10.                         // Remove top activity
  11.                         if (task.baseIntent.hasCategory(Intent.CATEGORY_BROWSABLE))
  12.                                 continue;
  13.                         Intent intent = new Intent(Intent.ACTION_MAIN);
  14.                         intent.addCategory(Intent.CATEGORY_HOME);
  15.                         ResolveInfo ri = pm.resolveActivity((intent),1);
  16.                         if (ri != null && riList.size() < 10)
  17.                                 riList.add(ri);
  18.                 }
  19.         recentTasks.clear();
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

But i am not able to kill the browser activity(i.e the browser is opened my application). and still the browser activity is the top activity.
If some one knows the solution, please give reply.

Thanks
Shailaja
shailaja
Developer
Developer
 
Posts: 40
Joined: Fri Aug 06, 2010 8:00 am

Re: how to close a browser programatically

Postby Phyll » Thu Mar 22, 2012 9:15 pm

Hi shailaja,

Haven't been on here for a while I guess. Have you seen this explanation of what you ask about:

http://stackoverflow.com/questions/2720 ... ess-killer

You should be able to kill a process without any trouble as long as you have the process ID. No special permissions. Generally not recommended but not difficult.

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: MSNbot Media and 25 guests