一.Activity的概念与Activity的生命周期图:
18364230.jpg
二.Activity的创建流程
48768883.jpg.png
三.启动一个Activity的几种方式
显式启动:通过包名来启动,写法如下:
①最常见的:
startActivity(newIntent(当前Act.this,要启动的Act.class));
②通过Intent的ComponentName:
ComponentName cn = new ComponentName("当前Act的全限定类名","启动Act的全限定类名") ;
Intent intent = new Intent() ;
intent.setComponent(cn) ;
startActivity(intent) ;
③初始化Intent时指定包名:
Intent intent = new Intent("android.intent.action.MAIN");intent.setClassName("当前Act的全限定类名","启动Act的全限定类名");startActivity(intent);
2.隐式启动:通过Intent-filter的Action,Category或data来实现 这个是通过Intent的 intent-filter**来实现的,这个Intent那章会详细讲解! 这里知道个大概就可以了!
291262381.jpg.png
另外还有一个直接通过包名启动apk的:
Intentintent= getPackageManager().getLaunchIntentForPackage("apk第一个启动的Activity的全限定类名") ;if(intent!= null) startActivity(intent) ;
四、Activity间的数据传递:
1.传值
7185831.jpg.png
2.多个Activity间的交互(后一个传回给前一个)
67124491.jpg.png
3.知晓当前是哪个Activity
19579941.jpg.png
4.随时关闭所有Activity
59443692.jpg.png
5.为Activity设置过场动画
16878455.jpg.png
五、系统给我们提供的常见的Activity
//1.拨打电话// 给移动客服10086拨打电话Uriuri =Uri.parse("tel:10086");Intent intent =newIntent(Intent.ACTION_DIAL, uri);startActivity(intent);//2.发送短信// 给10086发送内容为“Hello”的短信Uriuri =Uri.parse("smsto:10086");Intent intent =newIntent(Intent.ACTION_SENDTO, uri);intent.putExtra("sms_body","Hello");startActivity(intent);//3.发送彩信(相当于发送带附件的短信)Intent intent =newIntent(Intent.ACTION_SEND);intent.putExtra("sms_body","Hello");Uriuri =Uri.parse("content://media/external/images/media/23");intent.putExtra(Intent.EXTRA_STREAM, uri);intent.setType("image/png");startActivity(intent);//4.打开浏览器:// 打开Google主页Uriuri =Uri.parse("http://www.baidu.com");Intent intent =newIntent(Intent.ACTION_VIEW, uri);startActivity(intent);//5.发送电子邮件:(阉割了Google服务的没戏!!!!)// 给someone@domain.com发邮件Uriuri =Uri.parse("mailto:someone@domain.com");Intent intent =newIntent(Intent.ACTION_SENDTO, uri);startActivity(intent);// 给someone@domain.com发邮件发送内容为“Hello”的邮件Intent intent =newIntent(Intent.ACTION_SEND);intent.putExtra(Intent.EXTRA_EMAIL,"someone@domain.com");intent.putExtra(Intent.EXTRA_SUBJECT,"Subject");intent.putExtra(Intent.EXTRA_TEXT,"Hello");intent.setType("text/plain");startActivity(intent);// 给多人发邮件Intent intent=newIntent(Intent.ACTION_SEND);String[] tos = {"1@abc.com","2@abc.com"};// 收件人String[] ccs = {"3@abc.com","4@abc.com"};// 抄送String[] bccs = {"5@abc.com","6@abc.com"};// 密送intent.putExtra(Intent.EXTRA_EMAIL, tos);intent.putExtra(Intent.EXTRA_CC, ccs);intent.putExtra(Intent.EXTRA_BCC, bccs);intent.putExtra(Intent.EXTRA_SUBJECT,"Subject");intent.putExtra(Intent.EXTRA_TEXT,"Hello");intent.setType("message/rfc822");startActivity(intent);//6.显示地图:// 打开Google地图中国北京位置(北纬39.9,东经116.3)Uriuri =Uri.parse("geo:39.9,116.3");Intent intent =newIntent(Intent.ACTION_VIEW, uri);startActivity(intent);//7.路径规划// 路径规划:从北京某地(北纬39.9,东经116.3)到上海某地(北纬31.2,东经121.4)Uriuri =Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4");Intent intent =newIntent(Intent.ACTION_VIEW, uri);startActivity(intent);//8.多媒体播放:Intent intent =newIntent(Intent.ACTION_VIEW);Uriuri =Uri.parse("file:///sdcard/foo.mp3");intent.setDataAndType(uri,"audio/mp3");startActivity(intent);//获取SD卡下所有音频文件,然后播放第一首=-=Uriuri =Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"1");Intent intent =newIntent(Intent.ACTION_VIEW, uri);startActivity(intent);//9.打开摄像头拍照:// 打开拍照程序Intent intent =newIntent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent,0);// 取出照片数据Bundle extras = intent.getExtras(); Bitmap bitmap = (Bitmap) extras.get("data");//另一种://调用系统相机应用程序,并存储拍下来的照片Intent intent =newIntent(MediaStore.ACTION_IMAGE_CAPTURE); time = Calendar.getInstance().getTimeInMillis();intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(newFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tucue", time +".jpg")));startActivityForResult(intent, ACTIVITY_GET_CAMERA_IMAGE);//10.获取并剪切图片// 获取并剪切图片Intent intent =newIntent(Intent.ACTION_GET_CONTENT);intent.setType("image/*");intent.putExtra("crop","true");// 开启剪切intent.putExtra("aspectX",1);// 剪切的宽高比为1:2intent.putExtra("aspectY",2);intent.putExtra("outputX",20);// 保存图片的宽和高intent.putExtra("outputY",40); intent.putExtra("output",Uri.fromFile(newFile("/mnt/sdcard/temp")));// 保存路径intent.putExtra("outputFormat","JPEG");// 返回格式startActivityForResult(intent,0);// 剪切特定图片Intent intent =newIntent("com.android.camera.action.CROP"); intent.setClassName("com.android.camera","com.android.camera.CropImage"); intent.setData(Uri.fromFile(newFile("/mnt/sdcard/temp"))); intent.putExtra("outputX",1);// 剪切的宽高比为1:2intent.putExtra("outputY",2);intent.putExtra("aspectX",20);// 保存图片的宽和高intent.putExtra("aspectY",40);intent.putExtra("scale",true);intent.putExtra("noFaceDetection",true); intent.putExtra("output",Uri.parse("file:///mnt/sdcard/temp")); startActivityForResult(intent,0);//11.打开Google Market// 打开Google Market直接进入该程序的详细页面Uriuri =Uri.parse("market://details?id="+"com.demo.app");Intent intent =newIntent(Intent.ACTION_VIEW, uri);startActivity(intent);//12.进入手机设置界面:// 进入无线网络设置界面(其它可以举一反三)Intent intent =newIntent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); startActivityForResult(intent,0);//13.安装apk:UriinstallUri =Uri.fromParts("package","xxx",null); returnIt =newIntent(Intent.ACTION_PACKAGE_ADDED, installUri);//14.卸载apk:Uriuri =Uri.fromParts("package", strPackageName,null); Intent it =newIntent(Intent.ACTION_DELETE, uri); startActivity(it);//15.发送附件:Intent it =newIntent(Intent.ACTION_SEND); it.putExtra(Intent.EXTRA_SUBJECT,"The email subject text"); it.putExtra(Intent.EXTRA_STREAM,"file:///sdcard/eoe.mp3"); sendIntent.setType("audio/mp3"); startActivity(Intent.createChooser(it,"Choose Email Client"));//16.进入联系人页面:Intent intent =newIntent();intent.setAction(Intent.ACTION_VIEW);intent.setData(People.CONTENT_URI);startActivity(intent);//17.查看指定联系人:UripersonUri = ContentUris.withAppendedId(People.CONTENT_URI, info.id);//info.id联系人IDIntent intent =newIntent();intent.setAction(Intent.ACTION_VIEW);intent.setData(personUri);startActivity(intent);
作者:Courage_SC
链接:http://www.jianshu.com/p/af4369e07d70
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。