- Activity profile
- Simple use of activity
- Jumps between the activity
- Intent to pass data
- Create activity get Return data
- How to call the system send SMS page
- How to send a text message
Activity profile
Activity is one of the four components of Android, which is used to display the interface. It provides a screen that the user can use to interact with to setContentView(View)
display the specified control.
In an Android app, an activity is usually a separate screen that shows some controls that can also listen and handle the user's events to respond. Intent
communication between the activity.
Simple use of activity
① Defining class Inheritance Activity
-
-
② declared in the
<application>
node of Androidmanifest.xml
<activity>
-
-
☆
<application>
: lable refers to the name of the program, and the name shown on the uninstall page is the lable designation.
-
-
☆ <activity>
in the lable refers to the activity's name, if this activity also specifies the following intent-filter
, then this interface will create a shortcut on the desktop, the shortcut name is the activity of the lable.
<category>
: Specifies the category of intent, which LAUNCHER
is to create a shortcut on the desktop.
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /></intent-filter>
-
-
Configuration of activity in the manifest file
-
-
①lable Property: The title of the activity page, the name of the interface, and if the interface is created, the shortcut name is the lable value.
-
-
②name Property: The specified value is: package name. Activity class name. If the package name is consistent with the mainfest packages, you can use "." To replace or not write.
-
-
③intent-filter child nodes: Add intent filtering, which can be initiated by implicit intent. You can generate shortcuts on your desktop to add an entry to your application.
-
-
④icon Properties: Specify an icon for the application
-
-
⑤android:theme Properties: Specify Theme Android:theme= "@android: Style/theme.dialog"
-
-
How to get a program to have multiple boot portals
-
Create one SecondActivity
and declare it in the manifest file, and modify <activity>
the value of two android:label
.
<applicationandroid:allowbackup="true"android:icon="@ Drawable/ic_launcher "android:label=" @string/app_name "android:theme= "@style/apptheme" > <activityandroid:name="com.bzh.launch.MainActivity"android:label= "I am the entrance 1" > <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activityandroid:name="com.bzh.launch.SecondActivity"android:label= "I am the entrance 2" > <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter> </activity></Application>
The test result diagram is as follows:
As we can see, there are two more app icons in the application manifest 我是入口1
and 我是入口2
, when opened separately, correspond to those declared in the manifest file Activity
.
Jumps between the activity
-
-
What is intent?
-
The
-
intent, which is used to activate the component, can be activated by the intent of the Android component.
-
-
The jump between activity is divided into two types
-
-
① explicit intent to jump: Know the class name of the jump target, and can get its bytecode object. Generally used in the interior of their own programs. Generally used in the interior of their own programs. An explicit jump cannot jump to a page in another program.
//创建一个Intent对象,并传递当前对象(Context对象)和要跳转的Activity类字节码new Intent(this, SecondActivity.class);//启动第二个ActivitystartActivity(intent);
-
-
② Implicit Intent Jump:: You can jump to another program's page in the current program. Implicit jumps do not need to refer to that class, but must know the action (action) and information (category) of that interface, so the activity that can be implicitly jumped must specify both the action and the information attribute when declared in the manifest file.
-
An
-
explicit intent is to specify a component when the intent is created, whereas an implicit intent does not specify a component, which matches the corresponding component by action, type, and data.
-
-
When we add the following filter for an activity,
intent-filter ; <!-- Set the action and category in the intent filter to start the activity when there is a matching action and category. The default category available with Android is-->, <action android:name = "com.itheima.activitySkip.SecondActivity" /> <category android:name = "Android.intent.category.DEFAULT" /> </intent-filter ;
-
-
If you want to activate the activity, you need to intent
specify the action and type in the
//创建一个Intent对象new Intent();//设置Actionintent.setAction("com.bzh.SecondActivity");//对于android.intent.category.DEFAULT类型的信息为Android系统默认的信息,省略也可以intent.addCategory("android.intent.category.DEFAULT");//启动ActivitystartActivity(intent);
Intent to pass data
Intent can also carry data through encapsulated bundle objects, in addition to activating components (initiating activity, etc.). So when you start an activity, you can also pass the data, and then in the new activity you get the intent object to get the data that the bundle holds.
Intent data types that can be passed are: eight basic data types, String, array, ArrayList<String>
bundle data Bundle, JavaBean to implement the serialization interface.
Implementation of serizlizable can be transmitted over the network
Implementation of Parcelable can be serialized into memory
Note: Intent passing too much data may result in a very slow jump speed or even a black screen, do not use intent to pass too much data, will affect the use of the application.
-
-
Passing data using the intent Filter data protocol
-
-
set the mimetype of data protocol and configuration data to achieve the purpose of transmitting data
-
① Configuring the intent filter in the manifest file
<activity android:name="com.bzh.SecondActivity"><!--Configure Intent filters --<intent-filter > <!--set the action and category in the intent filter to start the activity when there is a matching action and category <action android:name="com.bzh.SecondActivity"/> <!--set up data protocol -- <data android:scheme="Money"/> <!--configuration Data mimetype: Must be in xxx/xxx format, otherwise it will be reported as abnormal -- <data android:mimetype="Data/mymime"/> </intent-filter></activity>
-
The
-
② enables intent in fristactivity
and sets the data using the data protocol.
//Create a Intent object Intent Intent = new Intent (); //set action intent.setaction ( "com.bzh.SecondActivity "); //information for the Android.intent.category.DEFAULT type is the default information for the Android system, omitted or can be Intent.addcategory ( "Android.intent.category.DEFAULT" ); Intent.setdataandtype ( Uri.parse ( "money: Transfer $100. ), );
-
-
③ in SecondActivity
use getIntent()
to get to the intent and get the data
//获取从当前Context对象中获取IntentIntent intent = getIntent();//注意这个是getIntent()//获取数据String data = intent.getData().toString();System.out.println(data);
-
-
Using intent bundles to pass data
-
Google provides a number of overloaded methods in intent to putExtra(XX,XX)
help us pass the data, but let's look at the source code to find out that the underlying objects are all Bundle
implemented.
publicputExtrachar[] value) { ifnull) { new Bundle(); } mExtras.putCharArray(name, value); returnthis;}
Let's look at Bundle
the constructor and find that the bundle is actually aMap
publicBundle() { new HashMap<String, Object>(); mClassLoader = getClass().getClassLoader();}
-
-
Let's take a look at
Intent
how the
Bundle
data is passed in
-
-
On the send side
// Set data for Intent Intent.putextra ( "name" , "Zhang San" ); //to You intent sets the collection of string types Intent.putstringarraylistextra ( "list" , list); //declares a bundle object, inside the bundle is a wrapped map collection Bundle bundle = new bundle (); //bind data in the Bundle object bundle.putstring ( "pwd" , Span class= "hljs-string" > "123456" ); //to Intent Set bundle object Intent.putextras (bundle);
//pass bundle
through Putextras ()
-
-
Receiving end
Bundle extras = intent.getExtras();//通过getExtras获取bundle//从Bundle中获取可以为name的数据String name = (String) extras.get("name");//再通过get()获取具体值System.out.println("name="+name);//从Bundle对象中获取key为pwd的数据String pwd = (String) extras.get("pwd");System.out.println(pwd);//从Bundle中获取ley为list的数据List<String> list = (List<String>) extras.get("list");
Create activity get Return data
-
Operation Steps:
-
① Use the
startActivityForResult(Intent intent, int requestCode)
method to open the activity
-
② override
onActivityResult(int requestCode, int resultCode, Intent data)
method
-
③ after invoking the set return data in the open activity
setResult(int resultCode, Intent data)
, use
finish()
close acti Vity, the first activity invokes the
onActivityResult
method
-
Requestcode, the request code: used to distinguish which operation initiated the intent
-
ResultCode, the result code, which is used to distinguish which activity returned Data
How to call the system send SMS page
How can we pass on our data to the system's SMS page, like this
The code is actually very simple.
new Intent();// 设置发送短信的动作intent.setAction("android.intent.action.SEND");intent.addCategory("android.intent.category.DEFAULT");// 设置数据类型intent.setType("text/plain");// 使用Bundle传递数据intent.putExtra("sms_body", content);startActivity(intent);
But there are some key information (intent filter, key value), such as action, type, Putextra key value how to find? is actually a difficult point.
-
-
Next, let's talk about how to find these key information ideas. Now, if you want to have Android source code.
-
-
① We know that the Andriod has a 4-tier architecture, the top tier is the application layer, with many applications in the native system, and the short interest is one of the applications. In our source code, you can know that
android4.4src/packages/apps
our system comes with some of the application.
-
-
② Now that we know this, we'll find the directory where our text messages are.
android4.4src/pagkages/apps/mms/
-
③ we know that the activity in the manifest file is where the intent filter is defined, so let's take a look at the manifest file, and, since it's sending a text message, we'll search for the SEND
keyword to get the following result:
<activity android:name=". UI. Composemessageactivity "android:configchanges=" Orientation|screensize|keyboardhidden " Android:windowsoftinputmode="Statehidden|adjustresize"android:theme="@style/ Mmsholotheme "android:parentactivityname=". UI. Conversationlist "android:launchmode=" Singletop " > ...<intent-filter> <action android:name="Android.intent.action.SEND" /> <category android:name="Android.intent.category.DEFAULT" /> <data android:mimetype="image/*" /> </intent-filter> <intent-filter> <action android:name="Android.intent.action.SEND" /> <category android:name="Android.intent.category.DEFAULT" /> <data android:mimetype="Text/plain" /> </intent-filter>...</activity>
In this way, the key information of the intent filter is known, and the next is to Bundle
save the data in, then intent.putExtras(K,V)
the K value in how to find?
Since this is the application of the system, then the system code should have similar to intent.putExtras(K,V)
the code, and the above <activity name=‘...‘>
Name property value is .ui.ComposeMessageActivity
, we know the specific class is what, then we search for OK. The results are as follows:
How to send a text message
The procedure for sending a text message is fairly straightforward, see the following code:
//get SMS Manager Smsmanager Smsmanager = Smsmanager.getdefault (); //prevents text messages from being too long and splits the contents of arraylist<string> messagelist = smsmanager.dividemessage (content); for (String sms:messagelist) {//target person String destinationaddress = contact; //SMS Service Center address String scaddress = null ; //SMS content String Text = SMS; //deferred broadcast is generally null Pendingintent sentintent = null ; Pendingintent deliveryintent = null ; //use SMS Manager to send SMS smsmanager.sendtextmessage (destinationaddress, scaddress, text, Sentintent, deliveryintent);}
Of course, this is a very simple example, in more detail please see the android API
Android Basic Notes (vii)-page jump and data transfer