On an article, "Qtandroid detailed (1): Qandroidjniobject", we have done a lot of preparation, the purpose is to use the Qtandroid namespace in the StartActivity () method to call the Android system functionality to lay the foundation. So this time, we're going to study how to use the StartActivity method.
In my book, "QT on Android core programming, " The introduction of JNI, describing how to use JNI to extend Qt applications, is achieved by rewriting qtactivity, adding static methods to our Activity, actually from Qt After 5.3.0, there is a more convenient way, that is the next startactivity to introduce.
StartActivity, the method name already says its function: start an activity. Simply put, in Android, activity is an interactive interface that fills the window or hovers over other windows. Usually user-visible functions are associated with the activity, such as our use, Weibo, the United States, the interface we see, the operation of the east, are closely related to the activity.
OK, the background is so much, if interested, you can ask Niang, or go to https://developer.android.com/index.html to see the Android online documents. But it seems to be not open ha ...
Let's take a look at the StartActivity method in detail, and then for example, two cases of invoking an activity using startactivity.
StartActivity Method Detailed
The StartActivity method is prototyped as follows:
Voidstartactivity (const Qandroidjniobject & Intent, int receiverrequestcode, Qandroidactivityresultreceiver * Resultreceiver = 0);
As you can see, StartActivity has three parameters.
The first one is intent, we have introduced the intent class provided by Android in the example of "qtandroid (1): Qandroidjniobject", where the intent parameter is actually a Java intent object.
The second parameter is Receiverrequestcode, which is actually an identifier used to mark a startactivity call, and when you start an activity, the request code passes past, and when the activity that you call ends, You will get a notification that the request code you sent back to you, at the same time, this notice also brings back the Activity that you invoke the result of execution, called as result code. What's more, the Activity you invoke can also send more data back, and that data is carried by a Intent.
In order to handle the return result of your Activity, in Qt code we need a Qandroidactivityresultreceiver object, which is the third parameter.
In fact, the StartActivity method provided by Qt is a mashup, depending on the parameters, corresponding to the Android Activity class StartActivity (Intent) and Startactivityforresult (Intent , int) two methods. Look at the Android documentation, so it's clearer.
StartActivity and Startactivityforresult in Android
First look at StartActivity, which is the Activity class method, the prototype is as follows:
void StartActivity (Intent Intent);
There are two descriptions of startactivity in the Android documentation:
“
1. Launch a new activity. You won't receive any information on when the activity exits.
2. The StartActivity (Intent) method is used to start a new activity, which'll be placed at the top of the activity stack . It takes a single argument, an Intent, which describes the activity to be executed.
”
I translate, Chinese is as follows:
“
1. Start a new activity. When the new activity ends, you won't receive any messages.
2. The StartActivity (Intent) method is used to start a new activity that will be placed at the top of the activity stack. This method takes an intent (Intent) as a parameter and, as an argument, describes the information about the activity to be executed.
”
Oh, that's it, startactivity. Initiates a separate activity from the caller, and then dies, and the caller does not care about its fate or its consequences.
Then look at the Startactivityforresult method, which is also the Activity method. The prototype is as follows:
public void Startactivityforresult (Intent Intent, int requestcode);
It can be seen from the name, Startactivityforresult, that the purpose of initiating a new activity is to get the results of a new activity. In other words, it is very care for the new activity, you have to do something, you have to bring a result back to me. Let's say you pick a person from your address book and give it to me, and when you're done, if you don't pick anyone, tell me it's a failure, and if you pick a person, tell me that you're successful and that I have a place to get information about the person you picked.
The intent parameter has exactly the same meaning as the startactivity parameter, no need to mention it.
The Requestcode parameter, which is used to identify a call, is returned to the caller by the Onactivityresult () method when the new Activity finishes. The requestcode here is consistent with the second parameter of the startactivity provided by Qtandroid.
Then it's time to talk about how to receive the return of the new Activity.
Onactivityresult method of Activity class in Android
When we use Startactivityforresult to start a new activity, the goal is to do something to get some information, how do we know when the new activity is over, how it's done, what's going to bring back? Android has designed the Onactivityresult method for the activity class, and we can override this method when we extends the activity, distinguish a call according to Requestcode, judge the invocation result, extract the number of new activity According to
The Onactivityresult method is prototyped as follows:
protected void Onactivityresult (int requestcode, int resultcode, Intent data);
As you can see, it has three parameters.
The first parameter, Requestcode, is passed when we call Startactivityforresult.
The second parameter, ResultCode, is the execution result of the new Activity, in two ways, one is RESULT_OK (actually-1), and the other is result_failed (actually 0). These two values are a bit strange, not in line with my habits. When you are programming in C + +, do not use RESULT_OK and result_failed two constants, direct use of digital literal, it is easy to reverse (C's habit, 0 is ok,-1 is FAILED).
The third parameter is a Intent object that carries the data returned by the new Activity.
Intent in Android
More than once mentioned Android Intent, for Android developers to say Intent is very familiar with the existence of Qt developers to say Intent may be a little strange, let's take a bit of space to introduce.
Intent in the Android.content package, the full path class is named Android.content.Intent, which we also mentioned in the example of Qtandroid detailed (1): Qandroidjniobject.
The Intent class is a mechanism provided by Android for communication between components. It is an abstract description of the action to be performed, which can be used in conjunction with startactivity () to initiate an activity, with broadcastintent () to send a broadcast, or with StartService () or Bindservice () Mates to communicate with the background service.
The most common use of Intent is to initiate activities, and through Intent, you can invoke other system functions or functions provided by third parties, such as the ability to call calls, to display contacts, or to call the camera.
When we use Intent, we can specify an action that represents the action you want to do, which is what you want to do, and you can carry data to the called Party in Intent. The transferred party (activity or service) can obtain the data passed by the caller by means of Intent's GetData (), Getbundle (), Getxxxextra (). Intent is like a messenger between Android components that can tell us what to do and what data is available.
Intent has several constructors, we may use two of the following:
- Intent (String action)
- Intent (String action, Uri URI)
Intent also offers a number of ways to allow us to set up action and data, and here are just a few:
- Setaction (String action)
- PutExtra (String name, int value)
- PutExtra (String name, charsequence value)
- PutExtra (String name, Bundle value)
- SetData (Uri data)
To invoke a component with Intent, you need to specify the action, what exactly is the action?
The action is actually a string that represents a component, and when you pass a Intent object to StartActivity (), the Android framework parses the Intent action, finds the component that the action represents, and calls it. The process of parsing is more complicated, there are many rules, we can simply understand that there is a table for Android, register the various components of the system, when you give "Com.android.settings.SETTINGS" such action, Android will look in this table to set up the component and launch it. Intent has pre-defined many actions and is interested in viewing Intent's documents in the Android online Help.
Okay, so here's the background about startactivity in Android, and it's time to look back at our qtandroid.
Qandroidactivityresultreceiver
Qt uses startactivity a method that corresponds to the Android startactivity and Startactivityforresult two methods. When we provide the Requestcode and resultreceiver two parameters, the method corresponding to the Android Activity Startactivityforresult, and the Resultreceiver parameter is with Android A The Onactivityresult method of ctivity is adaptable, and its type is qandroidactivityresultreceiver.
Qandroidactivityresultreceiver is a pure virtual class that defines an interface that is prototyped as follows:
Virtual Voidhandleactivityresult (int receiverrequestcode, int resultcode, const qandroidjniobject & data) = 0;
See, the Handleactivityresult method is perfectly matched to the onactivityresult of the Android Activity. The only difference is that the third argument is converted to Qandroidjniobject, which is actually a Intent object.
To handle the return of the Activity, we need to implement the Qandroidactivityresultreceiver interface, such as:
Class Resultreceiver:public qandroidactivityresultreceiver{public: int requestid; Voidhandleactivityresult (int receiverrequestcode, int resultcode, const qandroidjniobject & Data) { if ( Receiverrequestcode = = RequestID) { if (ResultCode = = Result_ok) { //some code here } Else {//some code here}}} ;
It is important to note that Qandroidactivityresultreceiver is called asynchronously, so when you provide an object to receive the return result of a newly-tuned Activity, it is best to construct it from the heap (new), if you Qandroidactivityresultreceiver on the stack, which is likely to have disastrous consequences, when Qt receives the returned results of the Activity that is being tuned, the receiver you provide may have been deconstructed, and you are tragic and crashing.
Call Android Activity
I am good, finally come ...
OK, to talk about how to call Android Activity, it is not easy ah, foreshadowing long.
There are two ways to call Android Activity in Qt, depending on whether you are interested in the results:
- informed invocation, regardless of the result
- Cross-examine call, request result
Regardless of the result of the call may be Jiangzi:
Qandroidjniobject action = qandroidjniobject::fromstring ("Android.settings.SETTINGS"); Qandroidjniobject Intent ("Android/content/intent", "(ljava/lang/string;) V", action.object<jstring> ()); StartActivity (Intent, 0);
The call to follow the results may be jiangzi:
Resultreceiver *receiver = new Resultreceiver (1); Qandroidjniobject Action; Qandroidjniobject Intent ("Android/content/intent", "(ljava/lang/string;) V", action.object<jstring> ()); StartActivity (Intent, 1, receiver);
Unconsciously and write so long, it seems that the instance to wait until the next time ...
------------
Look Back:
- Qtandroid detailed (1): Qandroidjniobject
- Qt on Android column
Qtandroid (2): StartActivity and its little friends