Activity与Intent
android5個進程等級
1.Activity process(critical priority):活動進程(前臺進程)
2.Visible process(High priority):可見進程
3.Started Service process(High priority)
4.Background process(Low priority)
5.Empty process(Low priority)
退出進程:
System.exit(8);//系統進程正常退出
Process.killProcess(Process.myPid);//類似于垃圾回收
Activity創建過程:
1.建立Activity類及定義屬性及內部方法;
2.注冊Activity到manifast中;
3.在啟動中使用onCreat()實現業務:
(1)界面定義
(2)setContentView()加載頁面;
//將layout壓縮成View View view=this.getLayoutInflater().inflate(R.layout.activity_activity_demo, null);Intent:
Intent有兩種:Explicit intents(明確的意圖);Implicit intents?(含蓄的意圖)
Explicit?intents:通過組件名啟動
Implicit intents:通過匹配manifest file中的Intent filter來啟動組件,若匹配結果僅有一項,直接啟動;若多項匹配,根據彈出的dialog進行選擇
1.Explicit?intents
Intent屬性:Component:啟動Explicit intents必不缺少的
//顯示啟動Intent,必須指定Intent屬性Component,Component屬性需要指定ComponentName//1.直接創建時指定Intent intent=new Intent(ActivityDemoActivity.this, ActivityIntentDemo.class);Intent intent=new Intent();//2.通過ComponentName構造器來指定;只須指定包名與類名就可以唯一確定組件名ComponentName componentName=new ComponentName("androidstudy.ActivityDemo","androidstudy.ActivityDemo.ActivityIntentDemo");ComponentName componentName=new ComponentName(ActivityDemoActivity.this,ActivityIntentDemo.class);ComponentName componentName=new ComponentName(ActivityDemoActivity.this,"androidstudy.ActivityDemo.ActivityIntentDemo");intent.setComponent(componentName);//3.setClass()與setClassName()指定intent.setClass(ActivityDemoActivity.this, ActivityIntentDemo.class);intent.setClassName(ActivityDemoActivity.this, "androidstudy.ActivityDemo.ActivityIntentDemo");intent.setClassName("androidstudy.ActivityDemo","androidstudy.ActivityDemo.ActivityIntentDemo");startActivity(intent);返回Intent傳遞參數是否成功
調用startActivityForResult(intent, 1234);
intent是傳遞的Intent,1234為RequestResult,成功后回調函數protected void onActivityResult(int requestCode, int resultCode, Intent data) ;
在新的Activity 中通過調用函數setResult(5678,mIntent);設置回調的resultCode與Intent
//調用Intent的Activity public void demo(View view){ //顯示啟動Intent,必須指定Intent屬性Component,Component屬性需要指定ComponentName//1.直接創建時指定//Intent intent=new Intent(ActivityDemoActivity.this, ActivityIntentDemo.class);intent=new Intent();//2.通過ComponentName構造器來指定;只須指定包名與類名就可以唯一確定組件名//ComponentName componentName=new ComponentName("androidstudy.ActivityDemo","androidstudy.ActivityDemo.ActivityIntentDemo");//ComponentName componentName=new ComponentName(ActivityDemoActivity.this,ActivityIntentDemo.class);//ComponentName componentName=new ComponentName(ActivityDemoActivity.this,"androidstudy.ActivityDemo.ActivityIntentDemo");//intent.setComponent(componentName);//3.setClass()與setClassName()指定intent.setClass(ActivityDemoActivity.this, ActivityIntentDemo.class);//intent.setClassName(ActivityDemoActivity.this, "androidstudy.ActivityDemo.ActivityIntentDemo");//intent.setClassName("androidstudy.ActivityDemo","androidstudy.ActivityDemo.ActivityIntentDemo");intent.putExtra("STUDY", "hello 陽光 ");startActivityForResult(intent, 1234);}protected void onActivityResult(int requestCode, int resultCode, Intent data) {if(resultCode==5678){Toast.makeText(ActivityDemoActivity.this, data.getExtras().getString("STUDY"), Toast.LENGTH_LONG).show();System.out.println(intent.getStringExtra("STUDY"));}super.onActivityResult(requestCode, resultCode, data);}; //接收Intent的Activity public void click(View view){mIntent.putExtra("STUDY", "hello moon");//設置返回值setResult(5678,mIntent);//關閉此Activityfinish();}2.Implicit intents
//隱式啟動Intent//啟動電話Activity,Action:Intent.ACTION_DIAL Intent intent=new Intent();intent.setAction(Intent.ACTION_DIAL);intent.setData(Uri.parse("tel:15169091171"));startActivity(intent);//啟動電話Activity,Action:Intent.ACTION_CALLIntent intent=new Intent();intent.setAction(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:15169091171"));startActivity(intent);通過代碼分析?Action:Intent.ACTION_DIAL與Activity,Action:Intent.ACTION_CALL區別:
DIAL啟動的Action并不會直接撥號,需用戶進行確然撥號;
ACTION_CALL啟動的Action可直接進行撥號,但需要在manifest.xml文件中加上權限<uses-permission android:name="android.permission.CALL_PHONE"/>
Intent intent=new Intent();intent.setAction("moon.testDemo");intent.setData(Uri.parse("http://www.hao123.com"));startActivity(intent); xml:<activity android:name=".ActivityIntentDemo"><intent-filter><action android:name="moon.testDemo"/><category android:name="android.intent.category.DEFAULT"/><data android:scheme="http"/></intent-filter></activity>備注:1.雖然Activity中并沒有設置category屬性,但默認設置屬性為DEFAULT,所以在manifest.xml文件中要添加
<category android:name="android.intent.category.DEFAULT"/>,否則,運行會出現錯誤。
2.若 ? ? ? ?SaveInstanceState()函數會自動保存我們提供android:id的空間
2.狀態恢復onRestoreInstanceState()或onCreat():onResume()之前調用
備注:
1.重構onSaveInstanceState()與onRestoreInstanceState()函數時,需調用父類函數;
2.onRestoreInstanceState()函數不一定會被調用,一般恢復函數使用onCreat();
3.由于onSaveInstanceState()函數不一定會被調用,所以一般存儲短暫的狀態,存儲長期狀態一般使用onPause();
ConfigurationChanges
獲取configuration Configuration cfg=getResources().getConfiguration()
?
Activity基類:
FragmentActivity、AccountAuthenticatorActivity(實現賬戶管理界面)、TabActivity(Tab界面)、ListActivity(列表界面)、LauncherActivity(Activity列表界面)、PreferenceActivity(程序參數設置存儲界面)、AliasActivity(別名Activity基類,用于開始別人時結束自己)、ExpandableListActivity(可擴展列表界面)
?
轉載于:https://www.cnblogs.com/Eudora/p/3513663.html
總結
以上是生活随笔為你收集整理的Activity与Intent的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ecshop的smarty库还原成sma
- 下一篇: 数学:《线性代数》矩阵运算