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>
Related
I have just downloaded an app on google store and haven't run it. It automatically creates an app home shortcut at my home screen when downloading progress finishes. Every app on google store automatically do that ? If not? How can I do that?
I can create an app home shortcut when launching app at first time with this method:
public void createOrUpdateShortcut() {
appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
isAppInstalled = appPreferences.getBoolean("isAppInstalled", false);
String currentLanguage = Locale.getDefault().getDisplayLanguage();
String previousSetLanguage = appPreferences.getString("phoneLanguage", Locale.getDefault().getDisplayLanguage());
if (!previousSetLanguage.equals(currentLanguage)) {
shortcutReinstall = true;
}
if(!isAppInstalled || shortcutReinstall){
Intent HomeScreenShortCut= new Intent(getApplicationContext(),
BrowserLauncherActivity.class);
HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
HomeScreenShortCut.putExtra("duplicate", false);
if(shortcutReinstall) {
Intent removeIntent = new Intent();
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
String prevAppName = appPreferences.getString("appName", getString(R.string.app_name));
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, prevAppName);
removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(removeIntent);
}
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
//Make preference true
SharedPreferences.Editor editor = appPreferences.edit();
editor.putBoolean("isAppInstalled", true);
editor.putString("phoneLanguage", currentLanguage);
editor.putString("appName", getString(R.string.app_name));
editor.commit();
}
But I want to create a app home shortcut when installing app without launching because sometimes user don't open my app immediately at google store and they find the app home shortcut to open it after downloading progress finishes.
And I have another issue. My application title for homescreen shortcut can change on phone language change when launching app at first time with that code. But I want it changes immediately without launching.
Are there solutions for my problem? Thanks in advance.
But I want to create a app home shortcut when installing app without launching
That is not possible in general. Nothing of your code will run until either the user launches your launcher activity or something else uses an explicit Intent to start one of your components.
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);
}
I have an application A that wants to start an Activity in another application, B, which I don't own and cannot edit.
If B is already running and visible in recent apps, there's no problem in executing the wanted Activity of B using an Intent.
If B isn't running, I use the following code to execute its main Activity first, and then the one I want to execute:
String bPackage = "com.example.applicationb";
PackageManager pm = getPackageManager(this);
Intent main = pm.getLaunchIntentForPackage(bPackage);
Intent wanted = new Intent();
wanted.setPackage(bPackage);
wanted.setComponent(new ComponentName(bPackage,bPackage+".WantedActivity"));
main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
wanted.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
wanted.setExtras(mPreviouslyCreatedBundle);
startActivity(main);
startActivity(wanted);
The wanted Activity executes, but after some seconds I get an error and it stops working. Am I setting the Intents in a wrong way?
Make sure u have setted exported="true" for activity you are trying to redirect to another package
Basically my idea is when your second paackage app leave u need :
android.os.process.killprocess(android.os.process.mypid())
and when you launch use flag as start new task :
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
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();).
In fact, I am new to Android App Development. In my application, I have a couple of activities and I have provided my users with an exit option menu to be able to leave the application. But there is a problem. When they hit the Exit button, they are able to leave the application but when they enter the application for the second time, the page that they left off the last time will be launched.
Here comes my code:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0 :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Toast.makeText(this, "Goodbye Dear", Toast.LENGTH_LONG).show();
break;
Android Activity has two methods onPause and onDestroy where you can do the necessary cleanup.
http://developer.android.com/reference/android/app/Activity.html
Instead of using finish(), use System.exit(0);.
You have to override onPause and/or onDestroy methods inside your activity and delete your view within these methods.
The problem in your code is that Intent.FLAG_ACTIVITY_NEW_TASK doesn't remove your current Task. Read more about it here: Task and Back Stack | Android Developers.
Try using Intent.FLAG_ACTIVITY_CLEAR_TOP. From the documentation we can see that this gives the desired behavior.
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.