How to kill other apps? - android

Merged with Android-Close Other Apps.
How can I kill other applications? I can launch other applications using intents but can't find a way to kill them.
This launches the application:
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setComponent(
new ComponentName("com.bla.bla...",
"com.bla.bla.Main"));
context.startActivity(i);
I've tried to use the activity manager's kill
killBackgroundProcesses(String packageName)
but that does nothing.
I've also tried
appContext.stopService(intent)
but that did nothing

Related

Finish all activities and keep appearing in recent applications

I have used finishAffinity or below code because of finish all activities. But my application removed recent applications.
I want to finish all activities and keep appearing in recent applications. How can I do that?
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
System.exit(0);
It is used to remove a number of Activitys belonging to a specific
application from the current task (which may contain Activitys
belonging to multiple applications)
Try this:
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

How to launch another app or bring it back to foreground like Android launcher?

I am developing two apps that will be installed on the same device. My customer wants a shortcut button on each to jump to the other app in its current state. This action would duplicate the behavior of pressing HOME then pressing the other app's launcher icon. If the app has not been started, it would start it. If the app has already been started, then the current activity is resumed. Each app has many activities, so the current activity at the top of each app's task stack would be unknown at run-time. I have searched all over and have not found this problem answer sufficiently. I have tried variations on this code without success:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.example");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I am at a loss, any help is appreciated.
I figured out my problem. Here is what works:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyActivity"));
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

PendingIntent not launching /data apps

I have a keyguard appwidget where I'm launching a few different Intents. It seems I can launch /system apps (mms, settings etc. ) . but not /data apps ( chrome for example )
I have the following PendingIntentTemplate defined:
Intent startApplicationIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, startApplicationIntent, 0);
mainLayout.setPendingIntentTemplate(R.id.stackview, pendingIntent);
In my RemoteViewsFactory, I have the following FillInIntent:
Intent i = pm.getLaunchIntentForPackage(pkgName);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setAction(Intent.ACTION_MAIN);
stackviewRemoteView.setOnClickFillInIntent(R.id.stackview_image, i);
There are no errors, the intent just unlocks the keyguard.
It also definitely isn't null as the toString() method shows the following:
Intent { act=android.intent.action.MAIN cat=
[android.intent.category.LAUNCHER] flg=0x10000000
pkg=com.android.chrome
cmp=com.android.chrome/com.google.android.apps.chrome.Main }
Is there something special I need to do for non-system apps? I thought getLaunchIntentForPackage would give me everything I needed to launch an app the same way as it would launch if it were selected from the home screen.
Seems that the way I was doing this was making a few assumptions.
PendingIntent.getActivity calls startActivity(), which requires CATEGORY_DEFAULT to be specified.
The following code is probably 'more correct' and less likely to throw errors that CATEGORY_DEFAULT can't be found:
Intent i = new Intent();
i.setPackage(pkgName);
i.addCategory(Intent.CATEGORY_DEFAULT);

How to avoid multiple instance of activity starting from service

I am working in a service, on device boot my service starts along with application.
Now if service finds some restricted apps launched, it backgrounds my application, shows home, kill app, and start again activity to foreground. But it is creating multiple instances when starting activity on boot.
I want to start previously launched activity every time if it exists, I am using the following code:
//activity to background
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
//killing restricted app
Appmgr.killBackgroundProcesses(RunningP.processName);
//again start activity
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(this, TaxiPlexer.class);
intent.setComponent(cn);
startActivity(intent);
I have used FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY and others but they are giving me force close error. Above code only creates multiple instances ON BOOT
add this to manifest:
<activity android:name="yourActivity" launchMode = "singleTask" ></activity>

Application launcher icon is not deleted from Home screen when uninstalling android app

I'm using a similar codesnippet as shown below to add an application shortcut on the homescreen:
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());
shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut");
// Then, set up the container intent (the response to the caller)
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
this, R.drawable.app_sample_code);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
// Now, return the result to the launcher
setResult(RESULT_OK, intent);
There is no problem with creating the shortcut, but when uninstalling the app, the shortcut remains on the homescreen. When uninstalling other apps they all seem to also remove their corresponding homescreen shortcuts. This is what i try to achieve with my "created-by-code-shortcut-icon"
Does any of you Android experts here on Stackoverflow know whats needed to remove the app shortcut from the homescreen when the app is uninstalled ?
I found some related threads, but they do not provide me the solution for my problem, but please feel free to catch up:
[0] https://developer.android.com/intl/de/resources/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.html
[1] Remove application from launcher programatically in Android
[2] How to remove application shortcut from home screen on uninstall automatically?
I think you can try putting this action in the second Intent: "com.android.launcher.action.INSTALL_SHORTCUT"
This works for me, the launcher icon gets installed on the home screen, and when I uninstall the application, the icon is removed. Have been struggling some time with this.
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());
Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
this, R.drawable.launcher_icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
sendBroadcast(intent);
Hope this helps.
I had the same problem as well.
Finally, i've figured out that when creating application's shortcut, application's intent must contain the Intent.ACTION_MAIN action, otherwise the shortcut won't be removed from the home screen upon uninstalling the application (not the intent being used for installing the shortcut, which has the com.android.launcher.action.INSTALL_SHORTCUT action) .
Hope it helps.

Categories

Resources