1. In Android, please briefly describe the process of calling JNI. 1) Install and download Cygwin, download Android NDK 2) design of JNI interface in NDK project 3) use C/C++ to achieve local methods 4)JNI generate dynamic link library. So file 5) copy dynamic link library to Java project, call in Java project, run Java project

2. Describe the structure of an Android application. Android Application structures include: Linux Kernel(Linux Kernel), Libraries(system runtime Libraries or C/C ++ core Libraries), Application Framework(development Framework package), and Applications (core Applications).

3. If the background Activity is reclaimed by the system for some reason, how to save the current state before being reclaimed by the system? OnSaveInstanceState () Executes onSaveInstanceState() when one of the activities in your application is running and A new Activity B is actively or passively started. OnCreate () : savedInstanceState () : savedInstanceState () : savedInstanceState () : savedInstanceState (); If A is not reclaimed, onResume() will be executed and onCreate() will be skipped.

4. How do I exit the Activity? How to safely exit an Application where multiple activities have been invoked? For applications with a single Activity, it’s easy to exit and just finish(). Of course, methods like killProcess() and system.exit () are also available. However, for multi-activity applications, if you want to exit the last Activity after opening multiple activities, the above method is useless, because the above method is only to end an Activity. Of course, there are people online who say yes. It’s like when someone asks how to capture the Home key in an app, and someone says you can compare KEYCODE_HOME with keyCode, when in fact you can’t do that without changing the framework. So it’s best to try it out for yourself.

So, is there a way to just quit the entire app? Prior to 2.1, you could use the restartPackage method of ActivityManager. It can just end the entire application. Permissions are required for using android. Permission. RESTART_PACKAGES. Be careful not to be fooled by the name.

However, in 2.2, this method failed. A new method was added in 2.2, killBackgroundProcesses(), which requires the permission android.permission-kill_background_processes. Unfortunately, it, like the 2.2 restartPackage, doesn’t work as well as it should.

Another method, forceStopPackage(), is the system’s own application management method to force the end of the program. It requires permissions android.permission.force_stop_packages. The android:sharedUserId= “android.uid. System” attribute needs to be added. Unfortunately, this method is not public, it can only run in the system process, third-party programs can not call. Because you need to add LOCAL_CERTIFICATE := platform to android. mk. Android. Mk is used to compile programs under Android source code.

As you can see from the above, in 2.2, there is no way to end an application directly, but only indirectly.

Several methods are provided for reference:

1. Forced exit by throwing exceptions: This method makes the program Force Close by throwing exceptions. Validation is ok, but the problem is how to terminate the program without popping up the Force Close window.

2. Log opened Activities: Log each Activity you open. When you need to exit, just close each Activity.

3. Send a specific broadcast: When you want to end the application, send a specific broadcast, and when each Activity receives a broadcast, shut it down.

4. Recursive exit: Use startActivityForResult to start a new Activity, then flag it, process it in onActivityResult, and close the recursion.

In addition to the first one, are trying to find a way to end each Activity, indirectly achieve the goal. But it’s also not perfect. You may find that if your application sets a NoSensor for each Activity, the sensor will probably work between the two activities. But at least we got what we wanted, and it didn’t affect users.

For programming convenience, it is best to define an Activity base class that addresses these common problems.

5. How to enable and disable Service? Service in Android is similar to Service in Windows. Service generally has no user operation interface and is not easily detected by users when running in the system. It can be used to develop programs such as monitoring. one Step 1: Inherit the Service class

public class SMSService extends Service { }
Copy the code

Step 2: Configure the service in a node in the Androidmanifest.xml file:

<service android:name=".DemoService" />
Copy the code

two The context.startService () and context.bindService services cannot run on their own and need to be started by calling either context.startService () or context.bindService (). Both methods can start a Service, but they are used in different ways.

1. Start the service with the startService() method. There is no connection between the caller and the service, and the service still runs even if the caller exits. Using the bindService() method to enable the service, the caller is bound to the service and the service terminates once the caller exits.

2. Use the context.startService () method to start the service. If the service is not created, the system calls the onCreate() method of the service and then the onStart() method. If the service has already been created before the startService() method is called, calling the startService() method multiple times will not result in multiple service creation, but will result in multiple calls to the onStart() method. A service started with startService() can only be terminated by calling context.stopService (), and onDestroy() when the service is terminated.

3. Use context.bindService () to start the service. If the service is not created, onCreate() and onBind() will be called. The onUnbind() method of the service is called when the caller exits. The onDestroy() method is then called. If the service is already bound before calling bindService(), calling the bindService() method more than once does not result in creating the service and binding more than once (that is, the onCreate() and onBind() methods are not called more than once). If the caller wishes to unbind the service being bound, the unbindService() method can be called, which also causes the system to call the service’s onUnbind() — >onDestroy() method.