Activity
Activity 是所有程序的根本,所有程序流程都运行在Activity之中,所以Activity类是为你创建一个窗口的,它可以对你的用户见面进行处理。 Activity在系统中被Activity堆所管理,当一个新的Activity被运行的时候,它被放置在堆的顶端,并且成为了一个活动的 Activity。之前运行的Activity则在堆中被放在它的下面,将不能在新的Activity前面,直到那个新的Activity退出。(一直想把Activity翻译成中文,发现翻译成中文很别扭)
一个Activity有四个状态(按重要性依次排序):
如果一个Activity显示在屏幕上(也可以说是在堆的顶端),则称为Activity是活动的。如果一个Activity没有激活,但是仍然可见,则这个Activity处于暂停状态。这个状态是并没有死亡,但是可以被系统杀死。如果一个Activity完成,但是它仍然保持着它之前的状态和内存中的信息。如果一个Activity被暂停或者完成了 ,系统可以随时从内存中丢掉这个Activity。对于Android的生命周期和对垃圾的处理,我觉得充分的显示出使用Java作为应用程序API的优势,独特的声明管理机制对开发者和使用者提供了便利。以下是生命周期图,重点是对于生命周期的把握,状态的保持和恢复,还有一会将要介绍作为Activity中间传输的Intent。Activity中常用的函数有:SetContentView() 增加另外一个内容findViewById() 根据在XML中所View控件的id属性找到控件,次方法要加到onCreate中finish() 在Activity运行完应该被关闭时使用startActivity() 运行一个Activity其生命周期涉及的函数有:void onCreate(Bundle savedInstanceState)void onStart()void onRestart()void onResume()void onPause()void onStop()void onDestroy()注意的是,Activity的使用需要在Manifest文件中添加相应 的<Activity>,并设置其属性和intent-filter。Intentintent是提供Activity直接交互的抽象描述,可以被startActivty运行。intent提供了一个媒介,提供组件的相互调用,实现调用者和被调用者之间的解耦。intent中属性的设置
action —— 通常要执行的动作,比如ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, 等.data —— 执行动作要操作的数据,Android采用指向数据的一个URI来表示。对于不同的动作,其URI数据的类型是不同的(可以设置type属性指 定特定类型数据),如ACTION_EDIT指定Data为文件URI,打电话为tel:URI,访问网络为http:URI,而由content provider提供的数据则为content: URIs。category —— 被执行动作的附加信息。type —— 显式指定Intent的数据类型 (MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。component —— 指定Intent的的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。extras —— 是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子 邮件发送组件。理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者;另一种是隐式的 Intent,即Intent的发送者在构造Intent对象时,并不知道也不关心接收者是谁,有利于降低发送者和接收者之间的耦合。对于显式Intent,Android不需要去做解析,因为目标组件已经很明确,Android需要解析的是那些隐式Intent,通过解析,将Intent映射给可以处理此Intent的Activity、IntentReceiver或Service。Intent 解析机制主要是通过查找已注册在 AndroidManifest.xml中的所有IntentFilter及其中定义的Intent,最终找到匹配的Intent。在这个解析过程中,Android是通过Intent的action、type、category这三个属性来进行判断的,判断方法如下:• 如果 Intent指明定了action,则目标组件的IntentFilter的action列表中就必须包含有这个action,否则不能匹配;• 如 果Intent没有提供type,系统将从data中得到数据类型。和action一样,目标组件的数据类型列表中必须包含Intent的数据类型,否则 不能匹配。• 如果Intent中的数据不是content: 类型的URI,而且Intent也没有明确指定它的type,将根据Intent中数据的scheme (比如 http: 或者mailto:) 进行匹配。同上,Intent 的scheme必须出现在目标组件的scheme列表中。• 如果Intent指定了一个或多个 category,这些类别必须全部出现在组建的类别列表中。比如Intent中包含了两个类别:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY,解析得到的目标组件必须至少包含这两个类别。Intent-Filter的定义范例:<activity android:name="NotesList" android:label="@string/title_notes_list"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.EDIT" /> <action android:name="android.intent.action.PICK" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.GET_CONTENT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-filter></activity>我的intent简单的实现:Activity02.javapackage dan.activity;import android.app.Activity;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class Activity02 extends Activity { Button myButton = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton = (Button) findViewById(R.id.myButton); myButton.setText("跳转到另一个Activity"); myButton.setOnClickListener(new MyButtonListener()); } class MyButtonListener implements android.view.View.OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub //生成一个Intent对象 Intent intent = new Intent(); intent.setClass(Activity02.this,OtherActivity.class); intent.putExtra("testExtra", "haha"); Activity02.this.startActivity(intent); } }}OtherActivity.javapackage dan.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class OtherActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { TextView myTextView = null; // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); myTextView = (TextView)findViewById(R.id.myTextView); Intent intent = getIntent(); String mytext = intent.getStringExtra("testExtra"); myTextView.setText(mytext); }}AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="dan.activity" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Activity02" 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="OtherActivity" android:label = "@string/other"></activity></application></manifest> main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>other.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:id = "@+id/myTextView" android:layout_width = "fill_parent" android:layout_height="wrap_content"/></LinearLayout>string.xml<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, Activity02!</string> <string name="app_name">Activity02</string> <string name = "other">otherActivity</string></resources>运行效果:点击按钮之后:下面是转载来的其他的一些Intent用法 实例(转自javaeye)显示网页 1. Uri uri = Uri.parse("http://google.com"); 2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 3. startActivity(it);显示地图 1. Uri uri = Uri.parse("geo:38.899533,-77.036476"); 2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 3. startActivity(it); 4. //其他 geo URI 範例 5. //geo:latitude,longitude 6. //geo:latitude,longitude?z=zoom 7. //geo:0,0?q=my+street+address 8. //geo:0,0?q=business+near+city 9. //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom路径规划 1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat startLng&daddr=endLat endLng&hl=en"); 2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 3. startActivity(it); 4. //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456 打电话 1. //叫出拨号程序 2. Uri uri = Uri.parse("tel:0800000123"); 3. Intent it = new Intent(Intent.ACTION_DIAL, uri); 4. startActivity(it); 1. //直接打电话出去 2. Uri uri = Uri.parse("tel:0800000123"); 3. Intent it = new Intent(Intent.ACTION_CALL, uri); 4. startActivity(it); 5. //用這個,要在 AndroidManifest.xml 中,加上 6. //<uses-permission id="android.permission.CALL_PHONE" /> 传送SMS/MMS 1. //调用短信程序 2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 3. it.putExtra("sms_body", "The SMS text"); 4. it.setType("vnd.android-dir/mms-sms"); 5. startActivity(it); 1. //传送消息 2. Uri uri = Uri.parse("smsto://0800000123"); 3. Intent it = new Intent(Intent.ACTION_SENDTO, uri); 4. it.putExtra("sms_body", "The SMS text"); 5. startActivity(it); 1. //传送 MMS 2. Uri uri = Uri.parse("content://media/external/images/media/23"); 3. Intent it = new Intent(Intent.ACTION_SEND); 4. it.putExtra("sms_body", "some text"); 5. it.putExtra(Intent.EXTRA_STREAM, uri); 6. it.setType("image/png"); 7. startActivity(it); 传送Email 1. Uri uri = Uri.parse("mailto:xxx@abc.com"); 2. Intent it = new Intent(Intent.ACTION_SENDTO, uri); 3. startActivity(it); 1. Intent it = new Intent(Intent.ACTION_SEND); 2. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com"); 3. it.putExtra(Intent.EXTRA_TEXT, "The email body text"); 4. it.setType("text/plain"); 5. startActivity(Intent.createChooser(it, "Choose Email Client")); 1. Intent it=new Intent(Intent.ACTION_SEND); 2. String[] tos={"me@abc.com"}; 3. String[] ccs={"you@abc.com"}; 4. it.putExtra(Intent.EXTRA_EMAIL, tos); 5. it.putExtra(Intent.EXTRA_CC, ccs); 6. it.putExtra(Intent.EXTRA_TEXT, "The email body text"); 7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); 8. it.setType("message/rfc822"); 9. startActivity(Intent.createChooser(it, "Choose Email Client")); 1. //传送附件 2. Intent it = new Intent(Intent.ACTION_SEND); 3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); 4. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3"); 5. sendIntent.setType("audio/mp3"); 6. startActivity(Intent.createChooser(it, "Choose Email Client"));播放多媒体 Uri uri = Uri.parse("file:///sdcard/song.mp3"); Intent it = new Intent(Intent.ACTION_VIEW, uri); it.setType("audio/mp3"); startActivity(it); Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);Market相关1. //寻找某个应用 2. Uri uri = Uri.parse("market://search?q=pname:pkg_name"); 3. Intent it = new Intent(Intent.ACTION_VIEW, uri); 4. startActivity(it); 5. //where pkg_name is the full package path for an application 1. //显示某个应用的相关信息 2. Uri uri = Uri.parse("market://details?id=app_id"); 3. Intent it = new Intent(Intent.ACTION_VIEW, uri); 4. startActivity(it); 5. //where app_id is the application ID, find the ID 6. //by clicking on your application on Market home 7. //page, and notice the ID from the address barUninstall应用程序1. Uri uri = Uri.fromParts("package", strPackageName, null); 2. Intent it = new Intent(Intent.ACTION_DELETE, uri); 3. startActivity(it);
转载自:http://www.360doc.com/content/10/1008/01/3639038_59221010.shtml#
www.taoyou100.cn 淘友100 满意100,提供给您最信赖的网络购物享受。