Android interview questions (5)-important

Source: Internet
Author: User

46. There are several ways to register broadcast. What are the advantages and disadvantages of these methods? Let's talk about the intention of introducing the broadcast mechanism to Android.

Android broadcast mechanism (two registration methods) in android, to accept broadcast information, we have to implement this broadcast receiver by ourselves. We can inherit BroadcastReceiver, you can have a broadcaster. There is not enough receiver. We have to rewrite the onReceiver method in BroadcastReceiver. What should we do when broadcasting? We need to implement it ourselves, but we can build an information firewall. Code: public class SmsBroadCastReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {Bundle bundle = intent. getExtras (); Object [] object = (Object []) bundle. get ("pdus"); SmsMessage sms [] = new SmsMessage [object. length]; for (int I = 0; I <object. length; I ++) {sms [0] = SmsMessage. createFromPdu (byte []) object [I]); Toast. makeText (context, "from" + sms [I]. getDispla The message consumption of yOriginatingAddress () + "is:" + sms [I]. getDisplayMessageBody (), Toast. LENGTH_SHORT ). show () ;}// terminate the broadcast. Here we can handle it a little and implement the SMS Firewall Based on the Number entered by the user. AbortBroadcast () ;}} when a broadcast receiver is implemented, you also need to set the type of information that the broadcast receiver receives. Here is the information: android. provider. telephony. SMS_RECEIVED we can register the broadcast receiver into the system to let the system know that we have a broadcast receiver. There are two types: Code Dynamic Registration: // generate broadcast processing smsBroadCastReceiver = new SmsBroadCastReceiver (); // instantiate the filter and set the broadcast IntentFilter intentFilter = new IntentFilter ("android. provider. telephony. SMS_RECEIVED "); // register broadcast BroadCastReceiverActivity. this. registerReceiver (smsBroadCastReceiver, intentFilter); one is in AndroidManifest. configure broadcast in xml <? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "spl. broadCastReceiver "android: versionCode =" 1 "android: versionName =" 1.0 "> <application android: icon =" @ drawable/icon "android: label = "@ string/app_name"> <activity android: name = ". broadCastReceiverActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category androi D: name = "android. intent. category. LAUNCHER"/> </intent-filter> </activity> <! -- Broadcast registration --> <receiver er android: name = ". smsBroadCastReceiver "> <intent-filter android: priority =" 20 "> <action android: name =" android. provider. telephony. SMS_RECEIVED "/> </intent-filter> </Cycler> </application> <uses-sdk android: minSdkVersion =" 7 "/> <! -- Permission Application --> <uses-permission android: name = "android. permission. RECEIVE_SMS "> </uses-permission> </manifest> the difference between the two registration types is: 1) The first is not a resident broadcast, that is, the life cycle of the broadcast following the program. 2) The second type is resident. That is to say, when the application is closed, the program will be automatically called and run if information is broadcast.

 

47. Explain the relationship among Message, Handler, Message Queue, and logoff in the single-thread model. Handler Introduction: A Handler allows you to send and process Message and Runable objects, which are associated with the MessageQueue of a thread. Each thread instance is associated with a separate thread and the MessageQueue of the thread. When you create a new Handler, it is bound with the thread that created it. Here, we can also understand the thread as the MessageQueue. From this point of view, Handler transfers the Message and Runable objects to MessageQueue, and when these objects leave MessageQueue, Handler is responsible for executing them. Handler has two main purposes: (1) determine to execute one or more Message and Runnable objects at a certain time point in the future. (2) Add some actions to be executed in other threads (not Handler binding threads. Scheduling Message, that is, (1), can be completed through the following method: post (Runnable): Runnable is executed on the thread bound to the handler, that is, no new thread is created. PostAtTime (Runnable, long): postDelayed (Runnable, long): sendEmptyMessage (int): sendMessage (Message): sendMessageAtTime (Message, long): sendMessageDelayed (Message, long ): the post action allows you to queue Runnable objects into MessageQueue. When MessageQueue receives these messages, execute them, of course, in a certain order. The sendMessage action allows you to queue Message objects. These Message objects contain some information. Handler's hanlerMessage (Message) processes these messages. of course, handlerMessage (Message) must be overwritten by the sub-class of Handler. This is what programmers need to do. When posting or sending is sent to a Hanler, you can perform the following three actions: When MessageQueue is ready, it is necessary to define a delay time and a precise time for processing. The latter two allow you to implement timeout, tick, and time-based behaviors. When your application creates a new process, the main thread (that is, the UI thread) comes with a MessageQueue, which manages the top-level application objects (such as activities and broadcast receivers) and the window created by the main thread. You can create your own thread and communicate with the main thread through a Handler. This is done through post and sendmessage, just like before. The difference is in which thread to execute this method. When appropriate, the given Runnable and Message will be Scheduled in the MessageQueue of Handler. Message description: The Message class defines an information that contains a descriptor and any data object. This information is passed to Handler. the Message Object provides two additional int fields and one Object field, which allows you to skip the allocation action in most cases. Although the Message constructor is public, the best way to obtain the Message instance is to call Message. obtain (), or Handler. obtainMessage () method. These methods obtain one from the recycle Object pool. MessageQueue Introduction: This is an underlying class that contains the message list. Logoff is responsible for distributing these messages. Messages are not directly added to a MessageQueue, but are associated with logoff through MessageQueue. IdleHandler. You can obtain MessageQueue from the current thread through logoff. myQueue. Logoff Description: The logoff class is used to execute the message loop in a thread. By default, no message loop is associated with a thread. In the thread, call prepare () to create a logoff, and then use loop () to process the messages until the loop ends. Most of the interactions with message loop are handled by Handler. The following is a typical thread implementation with logoff. Class LooperThread extends Thread {public Handler mHandler; public void run () {loid. prepare (); mHandler = new Handler () {public void handleMessage (Message msg) {// process incoming messages here}; Looper. loop ();}}

 

48. What is the full name of AIDL? How to work? What types of data can be processed? The full name of AIDL is Android Interface Define Language. When process A is going to call the service in process B and implement communication, we usually use AIDL to operate Project A: first, we are in the net. blogjava. mobile. create a RemoteService in the aidlservice package. the aidl file contains a custom interface containing the get method. The ADT plug-in will automatically generate a RemoteService. java file under the gen Directory, which contains an internal class named RemoteService. stub, which contains the get method of the aidl file interface. Description 1: the location of the aidl file is not fixed. You can define your own MyService class at will. In the MyService class, you can define an internal class to inherit the internal class RemoteService. stub to implement the get method. The onBind method returns the object of this internal class. The system will automatically encapsulate this object as an IBinder object and pass it to its caller. Configure the MyService class in the AndroidManifest. xml file. The Code is as follows: <! -- Register the service --> <service android: name = ". MyService"> <intent-filter> <! -- Specify the ID of the AIDL service to be called --> <action android: name = "net. blogjava. mobile. aidlservice. remoteService "/> </intent-filter> </service> Why do you need to specify the ID of the AIDL service to be called, that is, to tell the outside world that the MyService class can be accessed by other processes, as long as other processes know this ID, it is with this ID that Project B can find Project A to implement communication. Note: AIDL does not require permissions for Project B: first, we need to add the RemoteService generated in Project. copy the java file to Project B. Bind The aidl service to the AIDL service in the bindService method and use the RemoteService ID as the intent action parameter. NOTE: If we put the RemoteService. aidl file in a bag separately, copy the package under the gen directory to Project B. If we set the RemoteService. aidl files are stored together with other classes. Therefore, we need to create corresponding packages in Project B to ensure RmoteService. the java file registration is correct. We cannot modify RemoteService. java file bindService (new Inten ("net. blogjava. mobile. aidlservice. remoteService "), serviceConnection, Context. BIND_AUTO_CREATE); the service parameter in the onServiceConnected (ComponentName, IBinder service) method of ServiceConnection is that the MyService class in project A inherits RemoteService. the object of the internal class of the stub class.

 

49. Please explain the differences between the permissions for running Android programs and those for file systems. Permission for running Dalvik (android authorization) File System linux kernel authorization

 

50. If multiple browsers are installed on the system, can you specify a browser to access the specified page? Describe the reason. Directly send the Uri to pass the parameter, or use the data attribute 51 in the intentfilter in manifest. How do you evaluate the Android system? Advantages and disadvantages.

Answer: five advantages of mobile phones on the Android platform:

1. Openness in terms of advantages, the Android platform is development first. The developed platform allows any mobile terminal vendor to join the Android alliance. Significant openness allows more developers. as users and applications become increasingly diversified, a new platform will soon become mature. Openness is conducive to accumulating popularity for the development of Android. The popularity includes consumers and manufacturers. for consumers, benefiting greatly is the rich software resources. Open platforms will also bring greater competition, so that consumers can purchase their desired mobile phones at a lower price.

2. Break away from the limitations of operators for a long time in the past, especially in Europe and America, mobile apps are often restricted by operators, and almost all the functions used to access any network are controlled by operators. Since the iPhone went public last year, users can connect to the network more conveniently, reducing restrictions on operators. With the gradual transition and improvement of EDGE and HSDPA mobile networks from 2G to 3G, the random access to the network by mobile phones is no longer a joke among operators, when you can use the mobile phone IM software for instant chat, think back to the recent high-price lottery and image download business, is it like a nightmare? Android terminals promoted by Internet giant Google are inherently networked and will bring users closer to the Internet.

III. The wide selection of hardware is related to the openness of the Android platform. Due to the openness of Android, many manufacturers have released a variety of unique features. The differences and features in functions do not affect data synchronization or software compatibility. For example, you can switch from a Nokia Symbian mobile phone to an Apple iPhone, at the same time, it is also convenient to transfer the excellent software in Symbian to the iPhone for use, contacts and other information. Is it very convenient?

4. The Android platform of a developer without any restrictions provides a very broad and free environment for third-party developers without any restrictions. It is conceivable how many novel and unique software will emerge. But it also has two sides. The bloody, violent, and erotic apps and games such as controllable are just one of the challenges left for Android.

 

5. seamlessly integrated Google applications today, Google has gone through 10 years of history, from search giant to full Internet penetration, google services, such as maps, emails, and searches, have become an important link connecting users and the Internet. Mobile phones on the Android platform will seamlessly integrate these excellent Google services.

Let's talk about the five major disadvantages of Android:

I. Security and Privacy the close connection between mobile phones and the Internet makes it difficult to keep personal privacy confidential. In addition to the personal footprint that is intentionally or inadvertently left during the Internet access process, Google is always standing behind you and penetrating everything. Therefore, the depth of the Internet will bring about a new round of privacy crisis.

2. Not the biggest operator that sells Android phones. T-Mobile released its first Android Mobile phone G1 in New York on the 23rd. However, in the North American market, the two largest carriers are AT&T and Verizon. Currently, only T-Mobile and Sprint are known to have the right to sell Android Mobile phones, among them, T-Mobile's 3G network is inferior to the other three. Therefore, users can purchase G1. whether they can experience the best 3G network service is another matter!

3. The operator is still able to influence the Android mobile phone market in China. Many users are dissatisfied with the purchase of mobile phone customization machines. They feel that the mobile phone they bought has been painted with advertisements. This situation also exists in foreign markets. Sprint, another Android mobile phone sales operator, will build its mobile phone store program in its model.

4. Fewer users of similar models in many mobile phone forums will have sub-forums for a specific model, share their experiences on using a mobile phone, and share software resources. For mobile phones on the Android platform, due to the wide variety of manufacturers and various product types, fewer and fewer users use the same model, and lack of program enhancement for unified models. For example, there are few discussions and groups dedicated to a specific model of a stockade, because of the proliferation and variety of devices in the stockade area, in addition to which features are eye-catching and popular models.

5. Excessive reliance on developers' lack of standard configurations when using the Windows Xp system on the PC end, a browser program such as Microsoft Windows Media Player is built in. You can choose a more diverse Player, such as Realplay or storm video. However, starting with the use of the recognition program can also meet a variety of needs. In the Android platform, because of its openness, the software is more dependent on third-party vendors. For example, the Android SDK does not have a built-in music player, and all depends on third-party development, lack of product uniformity.

 

52. What is ANR and how to avoid it?

A: ANR: Application Not Responding. In Android, the two system services, activity manager and window manager, are responsible for monitoring Application responses. When the following conditions occur, Android will display the ANR dialog box: Response to input events (such as buttons, touch screen events) more than 5 seconds intentReceiver (intentReceiver) the Android application has not been fully run in an independent thread (such as main) for more than 10 seconds ). This means that any operation that runs in the main thread requires a large amount of time will lead to ANR. At this time, your application has no chance to respond to input events and Intent broadcast ).

Therefore, any method running in the main thread should do as little work as possible. This is especially true for important methods such as onCreate () and onResume () during the lifecycle of an activity. Potential time-consuming operations,

Such as accessing networks and databases, or computing with high overhead, such as modifying the bitmap size, must be completed in a separate sub-thread (or using asynchronous requests, such as database operations ). However, this does not mean that your main Thread needs to enter the blocking state and has waited for the sub-Thread to end -- nor does it need to call the Therad. wait () or Thread. sleep () method. Instead, the main thread provides a Handler for the sub-thread so that the sub-thread can call it at the end of the day (xing: see the Snake example, this method is different from what we used previously ). Using this method involves your application, which ensures that your program responds well to the input and avoids the ANR generated because the input event is not processed for more than five seconds. This practice needs to be applied to all the threads that display the user interface, because they all face the same timeout issue.

 

53. What will cause Force Close? How to avoid it? Can I capture exceptions that cause them?

A: Generally, it is like a null pointer. You can check the logcat and resolve the error accordingly in the program.

 

55. Briefly explain activity, intent, intent filter, service, Broadcase, BroadcaseReceiver

A: An activity shows a Visualized User interface that users can operate. A service does not contain a visible user interface. Instead, it runs infinitely in the background and can be connected to a running service, after the connection, you can use the excuse exposed in the service to communicate with it. A broadcast receiver is a component that receives broadcast messages and responds. The broadcast receiver has no intent interface: the content provider is activated when receiving a ContentResolver request. Activity, service, and broadcast receiver are activated by asynchronous messages called intents. An intent is an Intent object that stores the message content. For activity and service, it indicates that the requested operation name and the URI Intent object of the data to be operated can explicitly specify a target component. In this case, android finds the component (based on the declaration in the manifest file) and activates it. However, if a target is not explicitly specified, android must find the optimal component that responds to the intent. It compares the Intent object with the intent filter of the target to complete this task. The intent filter of a component tells android that the component can process intent. Intent filter is also declared in the manifest file.

 

56. What are the advantages of IntentService?

A: benefits of IntentService * Acitivity processes. When processing Intent, a corresponding Service * Android process processor will be generated and you will not kill it as much as possible * it is very easy to use

 

57. What is the life cycle of the activity during screen switching?

1. When the android: configChanges of the Activity is not set, the split screen will call the life cycle again, and the split screen will be executed once, the screen is split twice. 2. When the android: configChanges = "orientation" of the Activity is set, the screen is re-called for each lifecycle, the function is executed only once when the horizontal and vertical screens are switched. 3. When the android: configChanges = "orientation | keyboardHidden" of the Activity is set, the function does not call the lifecycle of the Activity again, only the onConfigurationChanged method is executed. db file) is released together with the apk file? A: You can copy the dictionary. db file to the res aw directory of the Eclipse Android project. All files in the res aw directory will not be compressed, so that files in the directory can be extracted directly. You can copy the dictionary. db file to the res aw directory.

 

58. How can I open the database files in the res aw directory?

A: In Android, you cannot directly open the database file in the res aw directory. Instead, you need to copy the file to a directory on the phone memory or SD card when the program is started for the first time, then open the database file. The basic method of copying is to use the getResources (). openRawResource method to obtain the resource InputStream object in the res aw directory, and then write the data in the InputStream object to the corresponding file in other directories. You can use SQLiteDatabase. openOrCreateDatabase In the Android SDK to open SQLite database files in any directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.