Get All Activities by using package Name - android

I want to get all activities present in Application as a list by using PackageInfo. Please tell me is there any way to do this.
Thanks in advance.

I got answer to my question as follows.
public static ArrayList<ActivityInfo> getAllRunningActivities(Context context) {
try {
PackageInfo pi = context.getPackageManager().getPackageInfo(
context.getPackageName(), PackageManager.GET_ACTIVITIES);
return new ArrayList<>(Arrays.asList(pi.activities));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}

Try below code:
final PackageManager pm = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appList = pm.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(pm));
for (ResolveInfo temp : appList) {
Log.v("my logs", "package and activity name = "
+ temp.activityInfo.packageName + " "
+ temp.activityInfo.name);
}

Related

Twitter PostActivity issue

This intent has stoppped working . It was working for 2 months
2 month ago class name has changed from "com.twitter.android.PostActivity" to "com.twitter.applib.PostActivity" . I think it changed again. Is it changed again again and again ?
Can anyone help me to post tweet ?
and sorry about my english
try {
getPackageManager().getPackageInfo(
"com.twitter.android", 0);
Intent twitterIntent = new Intent(
Intent.ACTION_VIEW);
String twitterVersionName = getPackageManager()
.getPackageInfo("com.twitter.android",
0).versionName;
VersionControl currentVersion = new VersionControl(
twitterVersionName);
VersionControl requestedVersion = new VersionControl(
"4.1.9");
if (currentVersion.compareTo(requestedVersion) > -1) {
twitterIntent.setClassName(
"com.twitter.android",
"com.twitter.applib.PostActivity");
} else {
twitterIntent.setClassName(
"com.twitter.android",
"com.twitter.android.PostActivity");
}
twitterIntent.putExtra(Intent.EXTRA_TEXT,
tweetText);
startActivity(twitterIntent);
} catch (NameNotFoundException e) {
try {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/intent/tweet?source=webclient&text="
+ URLEncoder.encode(
tweetText, "UTF-8"))));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
Twitter change "com.twitter.applib.PostActivity" to "com.twitter.applib.composer.TextFirstComposerActivity"
You don't need to search for exact name , you can try like this,
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Your text here");
shareIntent.putExtra(Intent.EXTRA_STREAM, screenShot);
String appName = "twitter";
// use also instagram, pinterest, mail etc for appName.(not facebook)
final PackageManager pm = _activity.getPackageManager();
final List<?> activityList = pm.queryIntentActivities(shareIntent, 0);
int len = activityList.size();
Log.d("Tag","Length: "+len);
for (int i = 0; i < len; i++)
{
final ResolveInfo app = (ResolveInfo) activityList.get(i);
Log.d("Apps on share list: "+ app.activityInfo.name);
if ((app.activityInfo.name.contains(appName)))
{
Log.d("Tag","Found package: "+app.activityInfo.name);
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.setComponent(name);
//TODO change your activity here
_activity.startActivityForResult(shareIntent,SHARE_REQUEST_CODE);
return;
}
}
// not found so make default redirecting
Log.d("Tag","Not Found package: ");

How can I get the list of all device admins (active as well as inactive ) in android?

The Google API allows us to get the list of all active admins in the device from the method: getActiveAdmins(). However, my requirement is that I want the list of all possible admins in the device , whether active or not. Is there some method to do so ?
Thanks
You can find list of applications by below code
final Intent deviceAdminIntent = new Intent("android.app.action.DEVICE_ADMIN_ENABLED", null);
final List<ResolveInfo> pkgAppsList = getPackageManager().queryBroadcastReceivers(deviceAdminIntent, 0);
for (ResolveInfo aResolveInfo : pkgAppsList) {
String pkg = aResolveInfo.activityInfo.applicationInfo.packageName;
String name = aResolveInfo.activityInfo.applicationInfo.loadLabel(getPackageManager()).toString();
System.out.println("Package :: " + pkg);
System.out.println("Name :: " + name);
}
You can get all the necessary data in the ResolveInfo of an application. You can check ResolveInfo javadoc here.
I wrote a method which returns the ComponentName as same as getActiveAdmins
private List<ComponentName> getAllAdmins(Context mContext) {
List<ComponentName> result = new ArrayList<ComponentName>();
// Read all receivers who can listen android.app.action.DEVICE_ADMIN_ENABLED
// You can add all other action which can be used for DeviceAdminReceiver
final Intent deviceAdminIntent = new Intent("android.app.action.DEVICE_ADMIN_ENABLED", null);
final List<ResolveInfo> pkgAppsList = mContext.getPackageManager().queryBroadcastReceivers(deviceAdminIntent, 0);
for (ResolveInfo aResolveInfo : pkgAppsList) {
// Prepare component and add to list
result.add(new ComponentName(aResolveInfo.activityInfo.applicationInfo.packageName,
aResolveInfo.activityInfo.name));
//String pkg = aResolveInfo.activityInfo.applicationInfo.packageName;
//String name = aResolveInfo.activityInfo.applicationInfo.loadLabel(getPackageManager()).toString();
//System.out.println("Package :: " + pkg);
//System.out.println("Name :: " + name);
}
return result;
}
You can find the necessary code in the following Android source for DeviceAdminSettings: https://android.googlesource.com/platform/packages/apps/Settings/+/kitkat-release/src/com/android/settings/DeviceAdminSettings.java
DevicePolicyManager mDPM; // = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
final HashSet<ComponentName> mActiveAdmins = new HashSet<ComponentName>();
final ArrayList<DeviceAdminInfo> mAvailableAdmins = new ArrayList<DeviceAdminInfo>();
...
mActiveAdmins.clear();
List<ComponentName> cur = mDPM.getActiveAdmins();
if (cur != null) {
for (int i=0; i<cur.size(); i++) {
mActiveAdmins.add(cur.get(i));
}
}
mAvailableAdmins.clear();
List<ResolveInfo> avail = getActivity().getPackageManager().queryBroadcastReceivers(
new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED),
PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS);
if (avail == null) {
avail = Collections.emptyList();
}
// Some admins listed in mActiveAdmins may not have been found by the above query.
// We thus add them separately.
Set<ComponentName> activeAdminsNotInAvail = new HashSet<ComponentName>(mActiveAdmins);
for (ResolveInfo ri : avail) {
ComponentName riComponentName =
new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name);
activeAdminsNotInAvail.remove(riComponentName);
}
if (!activeAdminsNotInAvail.isEmpty()) {
avail = new ArrayList<ResolveInfo>(avail);
PackageManager packageManager = getActivity().getPackageManager();
for (ComponentName unlistedActiveAdmin : activeAdminsNotInAvail) {
List<ResolveInfo> resolved = packageManager.queryBroadcastReceivers(
new Intent().setComponent(unlistedActiveAdmin),
PackageManager.GET_META_DATA
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS);
if (resolved != null) {
avail.addAll(resolved);
}
}
}
for (int i = 0, count = avail.size(); i < count; i++) {
ResolveInfo ri = avail.get(i);
try {
DeviceAdminInfo dpi = new DeviceAdminInfo(getActivity(), ri);
if (dpi.isVisible() || mActiveAdmins.contains(dpi.getComponent())) {
mAvailableAdmins.add(dpi);
}
} catch (XmlPullParserException e) {
Log.w(TAG, "Skipping " + ri.activityInfo, e);
} catch (IOException e) {
Log.w(TAG, "Skipping " + ri.activityInfo, e);
}
}
getListView().setAdapter(new PolicyListAdapter());
This is how Android lists them for you in the Device Admins tab.

Checking if Facebook is installed on Android

How can I check that? Do i have to use PackageManager? Thank you :D
I check it this way (check if the returned value is unequal to null):
public static Intent findFacebookClient(Context con)
{
final String[] FacebookApps = {"com.facebook.android", "com.facebook.katana"};
Intent facebookIntent = new Intent();
facebookIntent.setType("text/plain");
final PackageManager packageManager = con.getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(facebookIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (int i = 0; i < FacebookApps.length; i++)
{
for (ResolveInfo resolveInfo : list)
{
String p = resolveInfo.activityInfo.packageName;
if (p != null && p.startsWith(FacebookApps[i]))
{
facebookIntent.setPackage(p);
return facebookIntent;
}
}
}
return null;
}
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Installed package : " + packageInfo.packageName);
}
Then you can check if packageInfo.packageName is equal to some string which contains the package name of that application.

How to display list of application names from package names

I am developing an application which show all current applications. I am getting the apps package names, thats good but not specific applications name How should I do it, I am using following code:
Context context = this.getApplicationContext();
ActivityManager mgr = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = mgr.getRunningTasks(30);
List<ActivityManager.RunningAppProcessInfo> tasksP = mgr.getRunningAppProcesses();
int numOfTasks = tasks.size();
for(int i = 0; i < numOfTasks; i++){
ActivityManager.RunningAppProcessInfo task = tasksP.get(i);
PackageInfo myPInfo = null;
try {
myPInfo = getPackageManager().getPackageInfo(task.processName, 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
Toast.makeText(location.this,
task.processName,
Toast.LENGTH_LONG).show();
}
Also please tell me how to display these apps name in check boxes, so that I can kill my desired app.
Try this code for getting Installed Application in your phone :
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> pkgAppsList = this.getPackageManager().queryIntentActivities( mainIntent, 0);
List<String> componentList = new ArrayList<String>();
for (ResolveInfo ri : pkgAppsList) {
if (ri.activityInfo != null) {
String app_name=ri.activityInfo.loadLabel(getPackageManager()).toString();
Log.d("Apps Name", ""+ri.activityInfo.loadLabel(getPackageManager()).toString());
}
}
}
Check this Link for further Reference :
Installed Application Details
Intent filter = new Intent(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_LAUNCHER);
Context context = getApplicationContext();
PackageManager manager = getPackageManager();
List<ResolveInfo> infos = getPackageManager().queryIntentActivities(filter,0);
List<Intent> filters = new ArrayList<Intent>();
filters.add(filter);
ComponentName component = new ComponentName(context.getPackageName(), MainActivity.class.getName());
List<ComponentName> activities = new ArrayList<ComponentName>();
//ComponentName[] components = new ComponentName[] {new ComponentName("//com.neo.application", "com.neo.application.Application"), component};
try {
manager.getApplicationInfo(//PACKAGE_NAME);//just Ctrl+space it u'll get the package names.
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Launch installed application with launch activity null

I am using the following code to get a list of installed applications and launch them.It only opens application like calculator or clock.When i try to open applications like contacts or camera it does not work as their launch activity is null.How do i open such applications.
In the below code s[5] points to the camera.
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages)
{
s[i]=packageInfo.packageName;
i++;
Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG,"Launch Activity :"+ pm.getLaunchIntentForPackage(packageInfo.packageName));
}
}
Intent mIntent = getPackageManager().getLaunchIntentForPackage(s[5]);
try {
startActivity(mIntent);
}
catch (Exception err) {
Toast t = Toast.makeText(getApplicationContext(),
"Not Found", Toast.LENGTH_SHORT);
t.show();
}
}
}
I finally able to solve the issue.The following code launches all the applications.
PackageManager pm = this.getPackageManager();
setContentView(R.layout.main);
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appList = pm.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(pm));
TextView c=(TextView)findViewById(R.id.textView1);
for(int i1=0; i1<appList.size(); i1++){
c.setText(c.getText() + "\n" +
"number: " + i1 + "\n" +
"Name: " + appList.get(i1).loadLabel(pm) + "\n"
);
}
Intent i11 = new Intent();
i11.setAction(Intent.ACTION_MAIN);
i11.addCategory(Intent.CATEGORY_LAUNCHER);
i11.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i11.setComponent(new ComponentName(appList.get (3).activityInfo.applicationInfo.packageName, appList.get(3).activityInfo.name));
startActivity(i11);

Categories

Resources