Bring my activity back up?

This question is a side issue of the proposal made in How to close or stop an external application that I started in Android .

I am writing an Android application that is a remote control for an industrial process - the process runs on a PC that constantly communicates with my Android application. Sometimes the PC sends the PDF file to Android, and I run AdobeReader.apk to display it. When the PC rejects the image, I want to reject it on Android.

In the link above, they told me that as soon as I started AdobeReader, I had no way to close it from my code. However, I could bring my application to the forefront, which is also good for my purposes. But I could not get it to work. The main application for my application is RemoteControlActivity, and I tried:

try { Intent i = new Intent(ctx, RemoteControlActivity.class); ctx.startActivity(i); } catch (ActivityNotFoundException e) { Log.d("ShowButtons(normal)", "Hide"); } 

I also tried adding aim.setFlags (...) before calling startActivity () with various combinations of Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_FROM_BACKGROUND with no luck.

In manifest start mode for remoteControlActivity singleTask

In the debugger, StartActivity () is called without landing in the Catch clause, but I did not hit the breakpoint in the RemoteControlActivity onRestart or onResume handlers.

Thanks in advance!

EDIT: The answer below showed a different flag, so I tried it:

 try { Intent i = new Intent(ctx, RemoteControlActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); ctx.startActivity(i); } catch (ActivityNotFoundException e) { Log.d("ShowButtons(normal)", "Hide"); } 

... but no luck - in the debugger, it calls startActivity, does not land in the catch block, but nothing happens.

Further editing : I was asked for a manifest; here is the part for the main activity:

 <activity android:launchMode="singleTask" android:label="@string/app_name" android:windowNoTitle="false" android:configChanges="orientation" android:screenOrientation="landscape" android:name="RemoteControlActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 
+6
source share
6 answers

The flag Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT set only by Android when it brings activity to the front itself. The setup itself does nothing.

The Intent.FLAG_FROM_BACKGROUND flag Intent.FLAG_FROM_BACKGROUND nothing, it is used only for informational purposes (to indicate that the activity was started by a background task).

You need to set Intent.FLAG_ACTIVITY_NEW_TASK . From the documentation for FLAG_ACTIVITY_NEW_TASK :

When using this flag, if the task is already running for the action you are starting now, then the new activity will not be started; instead, the current task will simply be brought to the forefront of the screen with the state of the latter.

+5
source

You can try the following:

 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
+2
source

If the activity is probably already running, use FLAG_ACTIVITY_REORDER_TO_FRONT . Or use both FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP .

+1
source

Your application cannot kill another third-party application, no matter how this external application was launched, if it does not use the same process as your application, or you rooted your device.

However, you can return application activity at the top (or foreground) even after starting an external third-party application using the application context rather than the activity context. This is done using the following method:

  private void bringMyAppToForeground(Context context) { Intent it = new Intent("intent.my.action"); it.setComponent(new ComponentName(getPackageName(), SplashScreenActivity.class.getName())); it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.getApplicationContext().startActivity(it); } 

where "intent.my.action" is defined in the manifest as follows:

 <activity android:name="com.example.SplashScreenActivity" > <intent-filter> <action android:name="intent.my.action" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 

Note that the action that caused the intention to launch the external application should not be such as to call the bringMyAppToForeground () method; instead, this method should be called from another action (or service), because otherwise it means that the activity is trying to start itself, and this will cause life cycle complications.

+1
source

The best option in this case is to kill the "adobe reader" process (or any other application). Using the android Intent class is useless in this case, since we are not dealing with actions in one application.

Killing a process can be performed in 2 stages in a root device with support :

1- Get the "adobe reader" pid: suppose adobe_reader_package_name is a string containing the name of the Adobe Reader package

  ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses(); int processid = 0; for (int i = 0; i < pids.size(); i++) { ActivityManager.RunningAppProcessInfo info = pids.get(i); if (info.processName.equalsIgnoreCase(adobe_reader_package_name)) { processid = info.pid; } } 

2- Killing a process using its pid:, it can be done using this method

  public static void doCmds(List<String> cmds) throws Exception { Process process = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); for (String tmpCmd : cmds) { os.writeBytes(tmpCmd+"\n"); } os.writeBytes("exit\n"); os.flush(); os.close(); process.waitFor(); } 

and calling it the following:

  List<String> cmdList = new ArrayList<String>(); cmdList.add("kill -9 "+processid); try { doCmds(cmdList); Log.d("A3", "kill process completed ..."); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } 

Hope this helps, cheers!

source-1: android: how to run a shell command from code

source-2: How to get pid of Android application using adb shell?

0
source

Please, use -

  FLAG_ACTIVITY_REORDER_TO_FRONT 

For example, if the current task is similar to A, B, C, D. And if D calls startActivity () for B with FLAG_ACTIVITY_REORDER_TO_FRONT , then B will be moved to the front, the new task will be similar - A, C, D, B.

0
source

Source: https://habr.com/ru/post/920328/


All Articles