Get Android category information of a running task - android

In Android, I can get the running tasks and activities using code like tis:
(ActivityManager.RunningTaskInfo) this.mActivityManager.getRunningTasks()
Then I can get the package name, class name etc.
My question is that whether there is a way to get the "category" information from it? Or anyone can suggest an alternative solution? for example, when I launch an app, the category is
cat=[android.intent.category.LAUNCHER]

I am not really sure what you mean, but maybe the following is what you want: retrieve the intent in your activity by calling getIntent(), and on the Intent object call the method getCategories(). This method will return a set with category strings.

I am not really sure, whether this would solve your problem, but you may use ActivityManager's getRecentTasks(int, int) API to get information about the recent tasks. It returns a list of ActivityManager.RecentTaskInfo objects, which have information about the intent used to launch the task. You can also use RecentTaskInfo.id field to map the task with the running tasks.

I found no direct way of doing it, but this may help:
Intent ApplicationIntent = new Intent(Intent.ACTION_MAIN);
ApplicationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> LauncherResolves = packageManager.queryIntentActivities(ApplicationIntent, 0);
List<String> AppsList = new ArrayList<String>();
for (int i = 0; i < LauncherResolves.size(); i++)
AppsList.add(LauncherResolves.get(i).activityInfo.name);
List<RunningTaskInfo> runningAppInfo = activityManager.getRunningTasks(10);
for (int i = 0; i < runningAppInfo.size(); i++)
{
if (AppsList.contains(runningAppInfo.get(i).topActivity.getClassName()))
{
// Do what ever you want
}
}

Related

How can I list all opened activities on Xamarin.Android?

I'm new to mobile development and I'm learning Xamarin. I already have some experience with C#.
I'm currently studying hierarchical navigation with Xamarin.Android.
In my project, I have the main activity, second and third activities where I navigate between them. I would like to know how can I list all opened activities?
For example, if I'm in the main activity and I request the list of opened activities, the function should return me a list with the name of the main activity only, but if I'm in the third activity, the function should return me a list containing the name of the main, second and third activities, more or less like this:
MainActivity
SecondActivity
ThirdActivity
How can I get this information?
I am working with Xamarin.Android and not Xamarin.Forms.
You could use ActivityManager to get the details of the Activities.
The GetRunningTasks would return a list of the tasks that are currently running. But this method was deprecated in API level 21. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks, and possibly some other tasks such as home that are known to not be sensitive.
For now, we only could use the BaseActivity to get the first activity and use the TopActivity to get the current one.
ActivityManager m = (ActivityManager)this.GetSystemService(ActivityService);
var runningTaskInfoList = m.GetRunningTasks(10);
foreach (var item in runningTaskInfoList)
{
string first = item.BaseActivity.ShortClassName;
string current = item.TopActivity.ShortClassName;
}
thanks for your help.
I think that's almost it. I made a small modification to your code, looking like this:
public string ListAllActivities()
{
string result = "";
ActivityManager activityManager = (ActivityManager)this.GetSystemService(ActivityService);
var runningTaskInfoList = activityManager.GetRunningTasks(10);
foreach (var item in runningTaskInfoList)
{
string first = item.BaseActivity.ShortClassName.Substring((item.BaseActivity.ShortClassName.LastIndexOf('.') + 1), item.BaseActivity.ShortClassName.Length - (item.BaseActivity.ShortClassName.LastIndexOf('.') + 1));
string current = item.TopActivity.ShortClassName.Substring((item.TopActivity.ShortClassName.LastIndexOf('.') + 1), item.TopActivity.ShortClassName.Length - (item.TopActivity.ShortClassName.LastIndexOf('.') + 1));
result += first + " - " + current + "\n";
}
return result;
}
And the result is this:Activities Stack
When I'm in the third activity, I don't get the list for the second activity. If I, being in the third activity, could list the second and main activities, the issue would be resolved.

pass boolean data between Android applications

I need to pass boolean data between Android applications, is there any simple method other than Sharedpreference or contentproviders. Please help me to find a solution !
With the use of Intent
you can pass boolean value from one application to another.
Explaining the code.
// Start as a main entry point, does not expect to receive data.
Intent it=new Intent(Intent.ACTION_MAIN);
// Return PackageManager instance to find global package information.
PackageManager manager = getPackageManager();
// Specifies the application to be called
it = manager.getLaunchIntentForPackage("com.your.app.packagename");
it.addCategory(Intent.CATEGORY_LAUNCHER);
// "key" is a parameter name, "value" is a true or false
params.putBoolean(key, value);
it.putExtras(params);
int requestCode = 0; // passing reuest code = 0
// Starts a main activity from specific package
startActivityForResult(it,requestCode);
// I hope that this topic be useful
You can use a socket connection between the apps. Here is a tutorial.
Use intents
intent.putExtras("value", true);
Boolean value= getIntent().getExtras().getBoolean("value");

Passing multiple data to an Activity?

I'm trying to pass multiple data items in one Intent:
if (strActStat == "Sedentary") {
// passactStat.putString("keySedentary", strActStat);
// passSeden.putString("keyMale", gender);
i = new Intent(CalorieTrackerTargetWeight.this, TargetWeightResults.class);
i.putExtra("keyGender", gender);
i.putExtra("keyAct", strActStat);
//i.putExtra("keyAct", strActStat);
startActivity(i);
}
Why doesn't this work? Why can't I pass multiple items in one Intent?
You can't compare strings with ==.
if (strActStat.equals("Sedentary")) { // should work
Edit:
#Hesam has written a pretty detailed answer but his solution is not really usable. Instead of using an ArrayList<String> you should stick with the putExtra(key, value). Why? Well there are some advantages over the ArrayList solution:
you are not limited to the type of the ArrayList
you are not forced to keep a static order in you list. As you can only work with index values to get a list you need to make sure that the put() was in the same order as get(). Think of the following case: You you often send 3 values, but in some cases you don't want to send the second value. When you use the ArrayList solution, you end up sending null as the second value to ensure that the third value will stay in his place. This is highly confusing coding! Instead you should just send two values and when the receiving activity tries to receive the second value, it can handle the returning null like it want... for example replace it with a default value.
Naming of the key will grant you the knowledge of always knowing what should be inside...
Your key should be declared in the receiving Activity as a constant. So you always know by looking at this constants what intent data the activity can handle. This is good programming!
Hope this helps in clarifying the intent usage a bit.
I think this is not the only problem, first, if (strActStat == "Sedentary") this is wrong. you can't compare to string in this way. Because in this way objects are comparing not the string. Correct way is if (strActStat.equalIgnoreCase("Sedentary")).
If you use Parcelable then you can pass multiple data in just 1 intent.
Also you can use ArrayList<String>.
Here is a skeleton of the code you need:
Declare List
private List<String> test;
Init List at appropriate place
test = new ArrayList<String>();
and add data as appropriate to test.
Pass to intent as follows:
Intent intent = getIntent();
intent.putStringArrayListExtra("test", (ArrayList<String>) test);
Retrieve data as follows:
ArrayList<String> test = data.getStringArrayListExtra("test");
Hope that helps.
Try this:
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
namevalue=name.getText().toString();
overvalue=over.getText().toString();
audiostatus=audio.getText().toString();
Intent intent=new Intent(Settings.this,home.class);
Bundle bundle = new Bundle();
bundle.putString( "namevalue",namevalue);
bundle.putString("overvalue",overvaluse);
bundle.putInt("value",variablename);
intent.putExtras(bundle);
startActivity(intent);
}
});
I faced the same problem.
My mistake was that one of the variable I was transferring was not initialized.
Like gender or strActStat in your case.

android accessing variables through classes

I have this application with 2 classes, in the first class I define a list array wich I want to access in my second class, how do I do that? class one (with the array extends listActivity and the other class extends Activity). I don't think it's nessecary to post my code as I believe there is a quick solution to this I just don't know it. Allthough I can post it if you can't help me without seeing the actual code.
You can use a custom ApplicationContext to share global state between activities - see here for more details...
you can pass ArrayList<String> 's across activities using intentExtras.
ArrayList<String> myArrayList = new ArrayList<String>();
// populate your array -> if you want to send objects make a string form of them
Intent myIntent = new Intent(this, Activit2.class);
mtIntent.putStringArrayList("my_array_list", myArrayList);
In Activity2
Intent intent = getIntent();
List<String> stringMediaList = intent.getStringArrayListExtra("my_array_list");
for (int i = 0; i < stringMediaList.size(); i++) {
//reconstruct object if needed
}
this will work if you dont want to use parcelable

Firing an Intent, but I can't seem to find out how to get the package name

Here is my current code:
public ListAdapter createAdapter() {
appNames = new String[] { "" };
appNamesList = new ArrayList<String>();
final PackageManager pm = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
packs = pm.queryIntentActivities(mainIntent, 0);
Collections.sort(packs, new ResolveInfo.DisplayNameComparator(pm));
for (int i = 0; i < packs.size(); i++) {
appNamesList.add(packs.get(i).loadLabel(pm).toString());
}
appNames = maker(appNamesList);
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, appNames);
return adapter;
}
I currently have this code updating a ListView for the user to select an app from the list. Now, on my Listener, I need to get the package name. Is there an easy way to get the package name from the code I already have? Or will I need to create another List of package names?
I feel dumb for asking this next question, but is there a way you can start an intent from just the app name? I already have that and it would be a lot easier.
Thanks.
Ok to achieve what you want you need to make some changes in your code.
Make package name part of your appNamesList like
appNameList = ArrayList<HashMap<String,String>>();
so when you will get the item in your listener using
listView.getAdapter().getItem(i);
you will get the package name as a part of this item.
now you can use it as you want.
If you are still confused or have any other doubt, let me know.
Are you able to call getPackageName() from the context of an Activity?

Categories

Resources