I wrote a program that opens access to the file system of the Android OS via the network. The problem is that although it should work all the time, it nevertheless closes after a while (if not active).

How can I make sure that the Java program does not close and work constantly?

And, I suppose, the same option is suitable for a C ++ program written using JNI ?

  • one
    oh, write a trojan? - VladD
  • who closes? Does the program go to sleep? or connection? - Gorets
  • Why is there a virus? Access to the file system is a useful thing, right now, debugging the program without suffering from connecting the phone to the computer for uploading, and downloading via wifi to the sdcard. I do not quite understand what is happening. My tcp connection with android is closing. In a normal situation, when can the program be completed? Now everything worked flawlessly and can not reproduce the error. And before that constantly fell off. It may well be that this is a program glitch and the android has nothing to do with it. - mikelsv

1 answer 1

Hello. There are a couple of ideas on how to do this:

  1. In the service (if you use it) to register, so that it loads immediately when the system starts.
  2. The program monitors the status of the service.
  3. In the program, during the call to the OnDestroy and OnStop , to make it collapse.

PS The 3rd way is eating a battery hard! A piece of code I can provide, but later. If I somewhere did not speak correctly, then correct.

Code:

1) ServiceBootStart.java

 public class ServiceBootStart extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { Intent serviceLauncher = new Intent(context, ServiceExample.class); context.startService(serviceLauncher); Log.v(this.getClass().getName(), "Service loaded while device boot."); } } } 

AndroidManifest.xml:

  <receiver android:name=".service.ServiceBootStart" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> `<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>` 

2) Well, on this point, then here is how much your imagination is enough. For example, I wrote to the file the entire log of the actions of the service, then the program read at what stage the service was, on what action it stopped, where the error took off, etc. Well, then did the logic of action in certain situations (but it's all very good. Hemorrhoids). You can do it easier, just browse the list of running programs, services and compare package names with the name of your service package, and then do certain actions.

The code to check if the service works:

 public boolean isServiceRunning(String serviceClassName){ ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE); for (RunningServiceInfo runningServiceInfo : services) { if (runningServiceInfo.service.getClassName().equals(serviceClassName)){ return true; } } return false; } 

3) The code, instead of quitting the application, just collapse it:

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) {// обрабатываю нажатие на кнопку "назад" if ((keyCode == KeyEvent.KEYCODE_BACK)) { moveTaskToBack(true); return true; } return super.onKeyDown(keyCode, event); } 

PS I don’t remember exactly the 3rd point, it seems to be the way it is done, there is simply no source at hand to look at.

  • one
    There is also an option to use the AlarmManager, try to start the service, say every five minutes if it fell. - Yura Ivanov
  • But could you provide the first and second code? And then I'm also very interesting). - Raskilas
  • Yes, I understood what to do in the form of a service. Do not tell me where to read, how to write it? I use the Service class, but I understand that there you need to indicate to the system that the program should not be closed. And how to turn off the program altogether, what function? - mikelsv
  • mikelsv Any article about services, these are the ones that once helped me blog.divenvrsk.org/2010/09/android.html?spref=tw and marakana.com/forums/android/examples/60.html How to make a program not I closed and wrote and provided the code (3rd way). Ummm ... I didn’t understand the last question a bit, you don’t need to close the program, but that’s okay, here’s one of the ways: system.exit(0) - tigr240172
  • Yeah, read the article did the service. I suffered for a long time with an amazing glitch: if you make a designer in the service class, the program will fall. By the way, here is the best service example: marakana.com/forums/android/examples/60.html It works right away. True, it still needs to be corrected a little ... I decided to do a little bit off: mikelsv.livejournal.com/7832.html . I still have one problem with services. My service runs the code through the native function that starts the stream. And there is such a suspicion that after some time the flow is killed. What can you read on this topic? - mikelsv