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);
//关闭此Activity
finish();
}
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_CALL
Intent 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(可扩展列表界面)