大家都精晓做程序猿不时会恶搞,如同android中,程序猿在setting中就暗藏那样生龙活虎项:
我们得以找到“关于手提式有线电话机”那风流倜傥项在中间有“android版本”那大器晚成项,如图:
一.Intent的介绍
当咱们超快点击“android版本”那后生可畏项时会弹出一张图片(恶搞型
,那是2.3操作系统,不过4.0类别的话会弹出三个android标识图片
Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来救助应用间的并行与报导,Intent担负对接纳中一遍操作的动作、动作涉及数额、附加数据开展描述,Android则基于此Intent的陈说,担任找到呼应的机件,将
Intent传递给调用的零零器件,并成功组件的调用。Intent不止可用来应用程序之间,也可用以应用程序内部的Activity/Service之间的竞相。由此,能够将Intent驾驭为不相同组件之间通讯的“媒介”特地提供组件互相调用的相干新闻。
,你按住android标记不放的话会现身过多android标记在活动的卡通:
二.Inten运行组件的措施
)。
Intent能够运行七个Activity,也能够运维多个Service,还能倡导二个广播布罗兹casts。具体方法如下:
此处大家就说说2.3系统的:
第生龙活虎我们找到Settings的源码,在package/app/Settings/src/com/android/settings下,大家要找到
组件名称 |
方法名称 |
Activity |
startActvity( ) startActivity( ) |
Service |
startService( ) bindService( ) |
Broadcasts |
sendBroadcasts( ) sendOrderedBroadcasts( ) sendStickyBroadcasts( ) |
DeviceInfoSettings.java这一个文件,正是“关于手提式有线电话机”那意气风发项的有关代码,在其的OnCreate方法中引用了二个xml文件:
复制代码 代码如下:
三.Intent的属性
addPreferencesFromResource(R.xml.device_info_settings);
Intent有以下多少个个性:
因此我们找到device_info_settings.xml文件看看:
动作(Action),数据(Data),分类(Category),类型(Type),组件(Compent)以致扩充信(Extra)。个中最常用的是Action属性和Data属性。
复制代码 代码如下:
1.Intent的Action属性
<!– Device firmware version –>
<Preference android:key=”firmware_version”
style=”?android:preferenceInformationStyle”
android:title=”@string/firmware_version”
android:summary=”@string/device_info_default”/>
Action是指Intent要水到渠成的动作,是一个字符串常量。SDK中定义了一些正规的Action常量如下表所示。
以此就是意味“android版本”的相干代码,我们能够看到它的Key是
复制代码 代码如下:
Constant |
Target component |
Action |
ACTION_CALL |
activity |
Initiate a phone call. |
ACTION_EDIT |
activity |
Display data for the user to edit. |
ACTION_MAIN |
activity |
Start up as the initial activity of a task, with no data input and no returned output. |
ACTION_SYNC |
activity |
Synchronize data on a server with data on the mobile device. |
ACTION_BATTERY_LOW |
broadcast receiver |
A warning that the battery is low. |
ACTION_HEADSET_PLUG |
broadcast receiver |
A headset has been plugged into the device, or unplugged from it. |
ACTION_SCREEN_ON |
broadcast receiver |
The screen has been turned on. |
ACTION_TIMEZONE_CHANGED |
broadcast receiver |
The setting for the time zone has changed. |
firmware_version
为此大家在java文件中就去找到它所被引述的代码,在DeviceInfoSettings.java中有一个 public
boolean onPreferenceTreeClick方法:
下边是八个测验Action常量的事例:
复制代码 代码如下:
main.xml
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
if (preference.getKey().equals(“firmware_version”)) {
System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
mHits[mHits.length-1] = SystemClock.uptimeMillis();
if (mHits[0] >= (SystemClock.uptimeMillis()-500)) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(“android”,
com.android.internal.app.PlatLogoActivity.class.getName());
try {
startActivity(intent);
} catch (Exception e) {
}
}
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
- <?xml version=”1.0″ encoding=”utf-8″?>
- <LinearLayout xmlns:android=””
- android:orientation=”vertical”
- android:layout_width=”fill_parent”
- android:layout_height=”fill_parent”
- >
- <TextView
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:text=”@string/hello”
- />
- <Button
- android:text=”测试Action属性”
- android:id=”@+id/getBtn”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- />
- </LinearLayout>
在内部就达成了你飞速点击“android版本”那生机勃勃项所达成的成效。
strings.xml
您也许感兴趣的随笔:
- Android分界面设计(应用程式设计方向
左边隐蔽菜单侧面突显content) - Android
呈现和遮掩输入法实今世码 - 深入分析android中回避与突显软键盘及不自动掸出键盘的兑现方式
- Android中规避标题栏和状态栏的主意
- Android键盘展现与掩饰代码
- 浅析Android中的visibility属性
- android
动态调控状态栏呈现和隐身的不二秘技实例 - Android完毕动态彰显或隐敝密码输入框的从头到尾的经过
- Android文本输入框(EditText)输入密码时呈现与潜伏
- Android开拓 — 控件的来得与隐敝 setVisibility View.VISIBLE
View.INVISIBLE View.GONE
- <?xml version=”1.0″ encoding=”utf-8″?>
- <resources>
- <string name=”hello”>测试Action属性</string>
- <string name=”app_name”>IntentActionDemo</string>
- </resources>
MainActivity.java
- package com.android.action.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private Button getBtn;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- getBtn=(Button)findViewById(R.id.getBtn);
- getBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_GET_CONTENT);// 设置Intent Action属性
- intent.setType(“vnd.android.cursor.item/phone”);// 设置Intent Type 属性
- //重假如获得通信录的剧情
- startActivity(intent); // 启动Activity
- }
- });
- }
- }
效果图:
2.Intent的Data属性
Intent的Data属性是实行动作的U瑞虎I和MIME类型,不一致的Action有例外的Data数据钦定。例如:ACTION_EDIT
Action应该和要编写制定的文书档案UENVISIONI
Data相配,ACTION_VIEW应用应该和要显示的U凯雷德I相称。
3.Intent的Category属性
Intent中的Category属性是一个试行动作Action的附加音讯。比如:CATEGOLacrosseY_HOME则意味着放回到Home分界面,ALTE君越NATIVE_CATEGO宝马7系Y代表如今的Intent是意气风发层层的可选动作中的多个。下表是SDK文书档案中关于Category的消息。
Constant |
Meaning |
CATEGORY_BROWSABLE |
The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message. |
CATEGORY_GADGET |
The activity can be embedded inside of another activity that hosts gadgets. |
CATEGORY_HOME |
The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed. |
CATEGORY_LAUNCHER |
The activity can be the initial activity of a task and is listed in the top-level application launcher. |
CATEGORY_PREFERENCE |
The target activity is a preference panel. |
上面是一个回去Home分界面包车型地铁例证:
main.xml
- <?xml version=”1.0″ encoding=”utf-8″?>
- <LinearLayout xmlns:android=””
- android:orientation=”vertical” android:layout_width=”fill_parent”
- android:layout_height=”fill_parent”
- >
- <TextView
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:text=”测试Intent Category”
- />
- <Button
- android:id=”@+id/Button1″
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:text=”转到Home界面”
- />
- </LinearLayout>
strings.xml
- <?xml version=”1.0″ encoding=”utf-8″?>
- <resources>
- <string name=”hello”>Hello World, MainActivity!</string>
- <string name=”app_name”>IntentCategoryDemo</string>
- </resources>
MainActivity.java
- package com.android.category.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private Button btn;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn = (Button)findViewById(R.id.Button1);
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_MAIN);// 添加Action属性
- intent.addCategory(Intent.CATEGORY_HOME);// 添加Category属性
- startActivity(intent);// 启动Activity
- }
- });
- }
- }
效果图:
4.Intent的Type属性
Intent的Type属性显式钦点Intent的数据类型(MIME卡塔尔国。经常Intent的数据类型能够基于数据我举办判别,可是透过安装那特天性,能够强制行使显式内定的品种而不再实行推理。
5.Intent的Compent属性
Intent的Compent属性内定Intent的的靶子组件的类名称。经常Android会依据Intent
中蕴藏的别的性质的音讯,举例action、data/type、category举行搜寻,最后找到三个与之合作的靶子组件。但是,假使component那么些特性有钦赐的话,将平素运用它钦命的零件,而不再进行上述查找进度。钦赐了那些天性未来,Intent的任何具备属性都以可选的。
6.Intent的Extra属性
Intent的Extra属性是加多一些零器件的附加新闻。比如,假设我们要通过一个Activity来发送三个Email,就足以通过Extra属性来增多subject和body。
上面包车型地铁事例在首先个Activity的EditText输入顾客名,该年龄保存在Intent的Extras属性中。当单击Button时,会在第3个Activity中体现顾客名。
first.xml
- <?xml version=”1.0″ encoding=”utf-8″?>
- <LinearLayout xmlns:android=””
- android:orientation=”vertical”
- android:layout_width=”fill_parent”
- android:layout_height=”fill_parent”
- >
- <TextView
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:text=”请输入客商名”
- />
- <EditText
- android:id=”@+id/EditText1″
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- />
- <Button
- android:id=”@+id/Button1″
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:text=”测试Extras属性”
- />
- </LinearLayout>
second.xml
- <?xml version=”1.0″ encoding=”utf-8″?>
- <LinearLayout xmlns:android=””
- android:orientation=”vertical”
- android:layout_width=”fill_parent”
- android:layout_height=”fill_parent”
- >
- <TextView
- android:id=”@+id/TextView1″
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- />
- </LinearLayout>
strings.xml
- <?xml version=”1.0″ encoding=”utf-8″?>
- <resources>
- <string name=”hello”>Hello World, FirstActivity!</string>
- <string name=”app_name”>IntentExtrasDemo</string>
- </resources>
FirstActivity.java
- package com.android.extras.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class FirstActivity extends Activity {
- private Button btn;
- private EditText etx;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.first);
- btn = (Button)findViewById(R.id.Button1);
- etx = (EditText)findViewById(R.id.EditText1);
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- //设置Intent的class属性,跳转到SecondActivity
- intent.setClass(FirstActivity.this, SecondActivity.class);
- //为intent加多额外的信息
- intent.putExtra(“useName”, etx.getText().toString());
- //启动Activity
- startActivity(intent);
- }
- });
- }
- }
SecondActivity.java
- package com.android.extras.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
- public class SecondActivity extends Activity {
- private TextView tv;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //设置当前的Activity的分界面布局
- setContentView(R.layout.second);
- //获得Intent
- Intent intent = this.getIntent();
- tv = (TextView)findViewById(R.id.TextView1);
- //从Intent得到额外音讯,设置为TextView的文本
- tv.setText(intent.getStringExtra(“useName”));
- }
- }
在乎:在丰盛其次个Activity
SecondActivity的时候,要在AndroidManifest.xml里面增加上SecondActivity,具体如下,就是在15行</activity>的背后增多上16~18行的代码。如若不那样做,就能够在模拟器上边世错误。
- <?xml version=”1.0″ encoding=”utf-8″?>
- <manifest xmlns:android=””
- package=”com.android.extras.activity”
- android:versionCode=”1″
- android:versionName=”1.0″>
- <uses-sdk android:minSdkVersion=”10″ />
- <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
- <activity android:name=”.FirstActivity”
- android:label=”@string/app_name”>
- <intent-filter>
- <action android:name=”android.intent.action.MAIN” />
- <category android:name=”android.intent.category.LAUNCHER” />
- </intent-filter>
- </activity>
- <activity android:name=”.SecondActivity”
- android:label=”@string/app_name”>
- </activity>
- </application>
- </manifest>
效果图: