I need to run my app in background - java

Whenever user open any app i need know if application is running on the UI thread. I also need to show a toast of the Package Name of that application.
I have user Activity Manager but it not working as it should. It always shows displayed Launcher.
ActivityManager manager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
pack = componentInfo.getPackageName();
Can anyone tell me how I can use usage stats to achieve this?

As far as displaying the name of your package you can use PackageManager and getLaunchIntentForPackage()

Related

Android: Two applications have same package name. How to differentiate them?

I'm trying to get every launchable applications in my device using this method:
apps = new ArrayList<>();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> availableActivities = manager.queryIntentActivities(intent, 0);
for(ResolveInfo ri:availableActivities){
AppDetail app = new AppDetail();
app.label = ri.loadLabel(manager);
app.name = ri.activityInfo.packageName;
app.icon = ri.activityInfo.loadIcon(manager);
apps.add(app);
}
I tried to print the label and package name of those application and found this:
Contacts com.sonyericsson.android.socialphonebook
Phone com.sonyericsson.android.socialphonebook
They have different app label yet the same package name. When I tried to open the apps, both of them open Contact app.
Is there any way to differentiate them? Or did I use a wrong method to get list of application?
queryIntentActivities retrieves all activities that can be performed for the given intent. So it can returns activites info with same package name.
I believe that Phone and Contacts are the same Contact app.
Two different icons can be created for the same program, one for each different activity. This makes sense, since the MAIN/LAUNCHER intent filter essentially tells android that the activity is the app's starting activity. So if you add this filter to two activities it will give you two icons for the same app to enter different activities. Nothing in android's intent filter model forces each app to have one and only one starting activity.

Initiliazing ParseLoginUI?

Where does one actually place the code to launch the ParseLoginUI activity?
ParseLoginBuilder builder = new ParseLoginBuilder(MainActivity.this);
startActivityForResult(builder.build(), 0);
Is it in the ParseLoginDispatchActivity? This was not made very clear at all within any of the official documentation:
https://github.com/ParsePlatform/ParseUI-Android
https://www.parse.com/docs/android/guide#user-interface
I'm importing ParseLoginUI into my existing app. What do I once I've installed everything, updated my manifests, my build.gradle and now want to actually launch the Login activity once my app launches?
Do I put something in my manifest to indicate that the ParseLoginActivity should launch first? That doesn't seem to work as an Activity from my main application is required to launch as the initial intent. I'm a little lost here... Any thoughts?
Well I did find one solution, albeit a trivial one:
Intent loginIntent = new Intent(MainActivity.this, ParseLoginActivity.class); startActivity(loginIntent);
I launched the above Intent with an options menu item, but you could do it with a button or whatever else suits your needs.
If you're importing ParseLoginUI into an existing app, it appears you can just launch ParseLoginActivity with a simple Intent. I wish they mentioned this on their integration tutorial. Seems like the most straightforward way to get it running.
This solution definitely launches the Activity you want, but it doesn't check for whether the user is logged in or not and hence doesn't redirect you to the appropriate pages in your log-in flow (which I believe has more to do with your Manifest). It does, however, allow you to successfully register a user and log in with Parse, which is a great start.
A better solution would be to add the following to the onCreate method in the Activity that launches when your app launches. So if when your app launches you land on FirstActivity, the following will check to see if you are logged in. If you are not, you will be sent the login screen, and if you are logged in you will be sent to the second Activity, which is presumably where your users will want to be when they open your app.
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
Intent launchMainActivity = new Intent(this, SecondActivity.class);
startActivity(launchMainActivity );
} else {
ParseLoginBuilder builder = new ParseLoginBuilder(FirstActivity.this);
startActivityForResult(builder.build(), 0);
}

Any way to close recent apps in code?

I want to build upon my battery saving android app and add the functionality to close all apps excluding user-specified ones.
Im not asking how to kill a process, I want to close the recent apps in the same way that you would by hitting your recent apps button and swiping them all away.
Does anyone know of a way to do this?
It is not recommanded but it is posible by using killBackgroundProcesses of Activitymanager.
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
//get a list of all installed apps.
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
if(packageInfo.packageName.equals("mypackage")) continue;
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
Hope it will helps you.

Closing an app from my app

I have developed an app which password protects another app ( say app A). So when I try to open app A, an activity pops on top of A prompting the user to enter password. Upon incorrect entry, it should close that activity and also close app A, which is directly under it.
Now I tried to do this using this code:
List<ActivityManager.RunningAppProcessInfo> pids = Unlock.am.getRunningAppProcesses();
for(int i = 0; i < pids.size(); i++)
{
ActivityManager.RunningAppProcessInfo info = pids.get(i);
if(info.processName.equalsIgnoreCase("com.A")){
pid = info.pid;
break;
}
}
android.os.Process.killProcess(pid);
But it does not work.
Later I realized that this is probably because the process of app A is not a direct child of my app's process ( that is, my app did not call app A). So is there anyway I can close but not necessarily kill app A from my app? What I mean to say is, killing app A is optional but closing it is mandatory.
I am not sure how to kill another app process. but you can take the user to home screen upon entering wrong password...
private void launchHomeScreen() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish(); // finish our Activity (based on your requirement)
}
Maybe this would work:
ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
activityManager.killBackgroundProcesses("com.A");
You'll need to add the following permission to your manifest.
<uses-permission> android:name="android.permission.KILL_BACKGROUND_PROCESSES" </uses-permission>

Start android app from sleeping device (not own activity)?

I'd like to start an app from a sleeping device.
First i do a wakelock to wakeup screen. But i cant get the device to unlock?
I know i can start my own activity with something like:
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
but as i'd like to start an 3rd party app app i cant use getWindow():
mContext.startActivity(mContext.getPackageManager().getLaunchIntentForPackage("com.sec.android.app.xy"));
Is there any way to set the flags before starting the activity?
If you know the third party's package and launcher activity names , this code should work (mPackage and mActivityName are those strings):
Intent LaunchIntent = new Intent();
LaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
LaunchIntent.setClassName(mPackage, mPackage + "." + mActivityName);
mContext.startActivity(LaunchIntent);
and mContext is the original application context (which you can instantiate as Context mContext = this.getApplicationContext();).

Categories