Android TaskManager (simple)

Tutorials with advanced 'difficulty' and more Lines of Code.

Android TaskManager (simple)

Postby plusminus » Tue Dec 18, 2007 9:07 pm

[align=center]This Tutorial was inspired by the awesome, but totally uncommented davanum Apache Blog.[/align]
[align=center]Android TaskManager (simple)[/align]

What is this: We will create a simple TaskManager ( better: Task-Lister as there is not yet real managing ).

:?: Problems/Questions: post right below...

Difficulty: 2 of 5

What it will look like:
[align=center]
Image[/align]

Description:
0. To read out the running Tasks we need a permission that we have to set in the AndroidManifest.xml of our current Project. If we would not set ('request') that permission our App would crash for security reasons.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission id="android.permission.GET_TASKS"/>  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

The whole AndroidManifest.xml looks like this:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    package="org.anddev.android.taskmanager">
  6.  
  7.     <uses-permission id="android.permission.GET_TASKS"/>  
  8.  
  9.     <application android:icon="@drawable/icon">
  10.  
  11.         <activity class=".TaskManager" android:label="@string/app_name">
  12.  
  13.             <intent-filter>
  14.  
  15.                 <action android:value="android.intent.action.MAIN" />
  16.  
  17.                 <category android:value="android.intent.category.LAUNCHER" />
  18.  
  19.             </intent-filter>
  20.  
  21.         </activity>
  22.  
  23.     </application>
  24.  
  25. </manifest>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


1. We will create one single/simpel function that will handle the whole setup. We call it updateTaskList(). It will grab the Systems IActivityManager. Request all tasks and display some info about them in a List.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @SuppressWarnings("unchecked")
  2.  
  3.         private void updateTaskList() {
  4.  
  5.                 /* Grab the Systems IActivityManager. */
  6.  
  7.                 IActivityManager myActivityManager = ActivityManagerNative.getDefault();
  8.  
  9.                 /* Will hold all the task"".toString()"" entries */
  10.  
  11.                 ArrayList<String> listEntries = new ArrayList<String>();
  12.  
  13.                 try {
  14.  
  15.                         int showLimit = 1;
  16.  
  17.                         /* Get all Tasks available (with limit set). */
  18.  
  19.                         List<IActivityManager.TaskInfo> allTasks = myActivityManager
  20.  
  21.                                         .getTasks(showLimit, 0, null);
  22.  
  23.                         int i = 1;
  24.  
  25.                         /* Loop through all tasks returned. */
  26.  
  27.                         for (IActivityManager.TaskInfo aTask : allTasks) {
  28.  
  29.                                 listEntries
  30.  
  31.                                                 .add("" + (i++) + ": "
  32.  
  33.                                                                 + aTask.baseActivity.getClassName() + " ID="
  34.  
  35.                                                                 + aTask.id);
  36.  
  37.                         }
  38.  
  39.                 } catch (DeadObjectException e) {
  40.  
  41.                         Log.e("TaskManager", e.getMessage(), e);
  42.  
  43.                 }
  44.  
  45.                 /* Display out listEntries */
  46.  
  47.                 setListAdapter(new ArrayAdapter<String>(this,
  48.  
  49.                                 android.R.layout.simple_list_item_1_small, listEntries));
  50.  
  51.         }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


[align=center]Thats it :)[/align]

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Re: Android TaskManager (simple)

Postby tum0rc0re » Wed Dec 19, 2007 8:00 am

As usual, cool article. plusminus, can u make some feature for adding any post in profile's bookmark? Else simply not comfortable to find old tutorials.
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby plusminus » Wed Dec 19, 2007 6:35 pm

Hello tum0rc0re,

you mean for every user a kind of favorites :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby tum0rc0re » Wed Dec 19, 2007 7:43 pm

plusminus wrote:Hello tum0rc0re,

you mean for every user a kind of favorites :?:

Regards,
plusminus


yes
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby xayide » Sat Feb 16, 2008 7:09 pm

Hello!

I have a question about the IActivityManager. I´d like to have a service always running, like if it was a daemon. I want this service to be inicialized when the emulator is inicialized. I have read over there that I can do this using IActivityManager, but reading your manual, I´m not very sure about that... Do you know how can I solve this problem?

thank you very much!
xayide
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 05, 2008 1:23 pm

documents and problem with IActivityManager.TaskInfo

Postby chouman » Wed Apr 16, 2008 2:26 am

Hey +-,

Have not looked android ever since the end of last year. Now i am coming back to play with it again. I have been looking around for some way to identify the activities from the users. I am specifically looking for phonebook opening and new contacts been added). Then i ran across this tutorial so i was trying it out but then i ran into problem with IActivityManager.TaskInfo, Eclipse can't find the library for this.

Also where do you find IActivityManager? I was looking over the class and package index and can't not find this at all. All i can see is ActivityManager

HOward
chouman
Developer
Developer
 
Posts: 27
Joined: Thu Jan 03, 2008 2:59 pm

Top

additional questions

Postby chouman » Wed Apr 16, 2008 4:15 am

also what's the difference between IActivityManager and ActrivityManager. It'll probably help if i can find out where the documentations for these classes
chouman
Developer
Developer
 
Posts: 27
Joined: Thu Jan 03, 2008 2:59 pm

Postby wiki » Thu Oct 02, 2008 9:31 am

ActivityManagerNative and IActivityManager manager is removed in 1.0?
how do I achieve the info of running tasks now?
regards
wiki
wiki
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Oct 01, 2008 7:04 am
Location: India

Postby ninor » Thu Oct 02, 2008 10:24 am

Right now, there are these APIs:

ActivityManager.RecentTaskInfo Information you can retrieve about tasks that the user has most recently started or visited.
ActivityManager.RunningServiceInfo Information you can retrieve about a particular Service that is currently running in the system.
ActivityManager.RunningTaskInfo Information you can retrieve about a particular task that is currently "running" in the system.


You can get lists of tasks (recent, running and services).
I suppose it's changed in some later release.
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby bino » Wed Oct 15, 2008 4:21 am

wiki wrote:ActivityManagerNative and IActivityManager manager is removed in 1.0?
how do I achieve the info of running tasks now?


I have found a way to port TasksManager to SDK 1.0, we will use ActivityManager, with instance is created from getSystemService(), like below:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
  3.  
  4. ArrayList<String> activityList = new ArrayList<String>();
  5.  
  6. List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(30);
  7.  
  8.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


you can use 30 or another number, it's the maximum number of running tasks you want to get.

Sorry, I'm not very good in expressing in English!
User avatar
bino
Freshman
Freshman
 
Posts: 9
Joined: Mon Oct 13, 2008 7:15 am

Postby adonnini » Thu Oct 16, 2008 9:20 pm

Hello,

I have been struggling to add permissions to the manifest file for the task manager application.

Apparently

<uses-permission id="android.permission.GET_TASKS"/>

does not work as it causes the following error:

Tag <uses permission> missing required attribute name
(I looked for information about the above error message but found
none)

If I change the entry to something like:

<uses-permission id="android.permission.GET_TASKS"
android:name="GET_TASKS">
</uses-permission>

the error disappears but the application crashes.

I also looked in
http://code.google.com/android/referenc ... mission....

it appears that the name and value of the constant are correct:
GET_TASKS, "android.permission.GET_TASKS"

Any help in resolving this issue would be greatly appreciated.

Thanks.

Alex

P.S. I have used the suggestion made in the last post to change the code to use ActvitiyManager
adonnini
Freshman
Freshman
 
Posts: 3
Joined: Thu Oct 16, 2008 1:57 am

Postby bino » Fri Oct 17, 2008 8:22 pm

@adonnini
you can try this:
<uses-permission android:name="android.permission.GET_TASKS"/>
User avatar
bino
Freshman
Freshman
 
Posts: 9
Joined: Mon Oct 13, 2008 7:15 am

Postby Taro » Wed Dec 03, 2008 1:57 am

hi, plusminus
If I want to show the corresponding app to the foreground by clicked the item in the list,how can i achieve that :?:
Taro
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Nov 25, 2008 9:08 am

task manager

Postby rahulp » Mon Sep 14, 2009 8:07 am

hi all.. can anyone please let me know how to capture timestamps opf activities say- how long was an activity foregraound or backraound, what are the sites opened from web, data passed.. etc.
rahul phadnis
rahulp
Developer
Developer
 
Posts: 30
Joined: Thu Feb 19, 2009 12:52 pm

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests