前几章参考:
1-引言
2-Objective-C 编程
3-类、对象和方法
4-数据类型和表达式
5-循环结构
6-选择结构
7-类
8-继承
9-多态、动态类型和动态绑定
10-变量和数据类型
11-分类和协议
12-预处理程序
13-基本的C语言特性
14-Foundation框架简介
15-数字、字符串和集合
16-使用文件
17-内存管理和自动引用计数
18-复制对象
在Objective-C语言中,归档是一个过程,即用某种格式来保存一个或多个对象,以便以后还原这些对象。(这个玩意类似于Java中的序列化和反序列化)
在Mac OS X上的应用程序使用XML属性列表(或plists)存储诸如默认参数选择、应用程序设置和配置信息这样的数据。
使用PropertyList Editor程序来创建属性列表。
使用NSPropertyListSerialization类在文件中写入或读取属性列表可以在不同的平台之间移植。
归档方法一般包括:
1)使用XML属性列表进行归档。(如果可能,尽量在程序中使用XML属性列表)
2)使用NSKeyedArchiver归档。
3)使用NSData创建自定义档案。
要归档当前没有列出的对象,必须告知系统如何归档(或编码)你的对象,以及如何解归档(或解码)它们。这是按照<NSCoding>协议,在类定义中添加encodeWithCoder:方法和initWithCoder:方法实现的。
每次归档程序想要根据指定的类编码对象时,都将调用encodeWithCoder:方法,该方法告知归档程序如何进行归档。类似地,每次从指定的类解码对象时,都会调用initWIthCoder:方法。
一般而言,编码方法应该指定如何归档想要保存的对象中的每个实例变量。
从档案文件中恢复数据很简单:所做的工作只需和归档文件相反。
首先,需要像以前那样分配一个数据空间。
其次,把档案文件中的数据读入该数据空间。
然后,需要创建一个NSKeydUnarchiver对象,并告知它从指定的空间解码数据。
必须调用解码方法来提取和解码归档的对象,做完之后,向NSKeyedUnarchiver对象发送一条finishDecoding消息。
使用归档程序复制对象:
可以使用Foundation的归档功能来创建对象的深复制。
Activity 生命周期
显式 Intent 调用
1 //创建一个显式的 Intent 对象(方法一:在构造函数中指定)
2 Intent intent = new Intent(Intent_Demo1.this, Intent_Demo1_Result1.class);
3
4 Bundle bundle = new Bundle();
5 bundle.putString("id", strID);
6 intent.putExtras(bundle);
7
8 intent.putExtra("name", "bbb");
9 intent.putExtra("userInfo", new UserInfo(1, "name"));
10 startActivity(intent);
11
12 //创建一个显式的 Intent 对象(方法二:用 setClass 方法)
13 Intent intent = new Intent();
14 Bundle bundle = new Bundle();
15 bundle.putString("id", strID);
16 intent.setClass(Intent_Demo1.this, Intent_Demo1_Result1.class);
17 intent.putExtras(bundle);
18 startActivity(intent);
19
20 //创建一个显式的 Intent 对象(方法三:用 setClass 方法)
21 Intent intent = new Intent();
22 Bundle bundle = new Bundle();
23 bundle.putString("id", strID);
24 intent.setClassName(Intent_Demo1.this, "com.great.activity_intent.Intent_Demo1_Result1");
25 intent.putExtras(bundle);
26 startActivity(intent);
27
28 //创建一个显式的 Intent 对象(方法四:用 setComponent 方法)
29 Intent intent = new Intent();
30 Bundle bundle = new Bundle();
31 bundle.putString("id", strID);
32 //setComponent方法的参数:ComponentName
33 intent.setComponent(new ComponentName(Intent_Demo1.this, Intent_Demo1_Result1.class));
34 intent.putExtras(bundle);
35 startActivity(intent);
Intent隐式跳转 Action
1 //创建一个隐式的 Intent 对象:Action 动作
2 /**
3 * 这里指定的是 AndroidManifest.xml 文件中配置的
4 * <intent-filter>标签中的<action android:name="com.great.activity_intent.Intent_Demo1_Result3" />
5 * 所在的 Activity,注意这里都要设置 <category android:name="android.intent.category.DEFAULT" />
6 */
7 Intent intent = new Intent();
8 //设置 Intent 的动作
9 intent.setAction("com.great.activity_intent.Intent_Demo1_Result3");
10 Bundle bundle = new Bundle();
11 bundle.putString("id", strID);
12 intent.putExtras(bundle);
13 startActivity(intent);
AndroidManifest.xml
1 <activity android:name="Intent_Demo1_Result3" 2 android:label="Intent_Demo1_Result3"> 3 <intent-filter> 4 <action android:name="com.great.activity_intent.Intent_Demo1_Result3" /> 5 <category android:name="android.intent.category.DEFAULT" /> 6 </intent-filter> 7 </activity>
Category 类别
1 //创建一个隐式的 Intent 对象:Category 类别
2 Intent intent = new Intent();
3 intent.setAction("com.great.activity_intent.Intent_Demo1_Result33");
4 /**
5 * 不指定 Category 或 只指定 AndroidManifest.xml 文件中 <intent-filter> 标签中配置的任意一个 Category
6 * <category android:name="android.intent.category.DEFAULT" /> 除外,就可以访问该 Activity,
7 */
8 intent.addCategory(Intent.CATEGORY_INFO);
9 intent.addCategory(Intent.CATEGORY_DEFAULT);
10 Bundle bundle = new Bundle();
11 bundle.putString("id", strID);
12 intent.putExtras(bundle);
13 startActivity(intent);
AndroidManifest.xml
<activity android:name="Intent_Demo1_Result2"
android:label="Intent_Demo1_Result2">
<intent-filter>
<category android:name="android.intent.category.INFO" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Date 数据 跳转
1 //创建一个隐式的 Intent 对象,方法四:Date 数据
2 Intent intent = new Intent();
3 Uri uri = Uri.parse("http://www.great.org:8080/folder/subfolder/etc/abc.pdf");
4
5 //注:setData、setDataAndType、setType 这三种方法只能单独使用,不可共用
6 //要么单独以 setData 方法设置 URI
7 //intent.setData(uri);
8 //要么单独以 setDataAndType 方法设置 URI 及 mime type
9 intent.setDataAndType(uri, "text/plain");
10 //要么单独以 setDataAndType 方法设置 Type
11 //intent.setType("text/plain");
12
13 /**
14 * 不指定 Category 或 只指定 AndroidManifest.xml 文件中 <intent-filter> 标签中配置的任意一个 Category
15 * <category android:name="android.intent.category.DEFAULT" /> 除外,就可以访问该 Activity
16 */
17 Bundle bundle = new Bundle();
18 bundle.putString("id", strID);
19 intent.putExtras(bundle);
20 startActivity(intent);
AndroidManifest.xml
1 <activity android:name="Intent_Demo1_Result2" 2 android:label="Intent_Demo1_Result2"> 3 <intent-filter> 4 <category android:name="android.intent.category.DEFAULT" /> 5 <data 6 android:scheme="http" 7 android:host="www.great.org" 8 android:port="8080" 9 android:pathPattern=".*pdf" 10 android:mimeType="text/plain"/> 11 </intent-filter> 12 </activity> 13
调用系统的的组件
//web浏览器
Uri uri= Uri.parse("http://www.baidu.com:8080/image/a.jpg");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
//地图(要在 Android 手机上才能测试)
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
//路径规划
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//拨打电话-调用拨号程序
Uri uri = Uri.parse("tel:15980665805");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
//拨打电话-直接拨打电话
//要使用这个必须在配置文件中加入<uses-permission android:name="android.permission.CALL_PHONE"/>
Uri uri = Uri.parse("tel:15980665805");
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
//调用发送短信程序(方法一)
Uri uri = Uri.parse("smsto:15980665805");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "The SMS text");
startActivity(intent);
//调用发送短信程序(方法二)
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("sms_body", "The SMS text");
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
//发送彩信
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "some text");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(intent);
//发送Email(方法一)(要在 Android 手机上才能测试)
Uri uri = Uri.parse("mailto:zhangsan@gmail.com");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(intent);
//发送Email(方法二)(要在 Android 手机上才能测试)
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:zhangsan@gmail.com"));
intent.putExtra(Intent.EXTRA_SUBJECT, "这是标题");
intent.putExtra(Intent.EXTRA_TEXT, "这是内容");
startActivity(intent);
//发送Email(方法三)(要在 Android 手机上才能测试)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "这是标题");
intent.putExtra(Intent.EXTRA_TEXT, "这是内容");
intent.setType("text/plain");
//选择一个邮件客户端
startActivity(Intent.createChooser(intent, "Choose Email Client"));
//发送Email(方法四)(要在 Android 手机上才能测试)
Intent intent = new Intent(Intent.ACTION_SEND);
//收件人
String[] tos = {"to1@abc.com", "to2@abc.com"};
//抄送人
String[] ccs = {"cc1@abc.com", "cc2@abc.com"};
//密送人
String[] bcc = {"bcc1@abc.com", "bcc2@abc.com"};
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_BCC, bcc);
intent.putExtra(Intent.EXTRA_SUBJECT, "这是标题");
intent.putExtra(Intent.EXTRA_TEXT, "这是内容");
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose Email Client"));
//发送Email且发送附件(要在 Android 手机上才能测试)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
intent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mp3/醉红颜.mp3");
intent.setType("audio/mp3");
startActivity(Intent.createChooser(intent, "Choose Email Client"));
//播放媒体文件(android 对中文名的文件支持不好)
Intent intent = new Intent(Intent.ACTION_VIEW);
//Uri uri = Uri.parse("file:///sdcard/zhy.mp3");
Uri uri = Uri.parse("file:///sdcard/a.mp3");
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
//音乐选择器
//它使用了Intent.ACTION_GET_CONTENT 和 MIME 类型来查找支持 audio/* 的所有 Data Picker,允许用户选择其中之一
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
//Intent.createChooser:应用选择器,这个方法创建一个 ACTION_CHOOSER Intent
startActivity(Intent.createChooser(intent, "选择音乐"));
Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
intent1.setType("audio/*");
Intent intent2 = new Intent(Intent.ACTION_CHOOSER);
intent2.putExtra(Intent.EXTRA_INTENT, intent1);
intent2.putExtra(Intent.EXTRA_TITLE, "aaaa");
startActivity(intent2);
// //设置壁纸
// Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
// startActivity(Intent.createChooser(intent, "设置壁纸"));
//卸载APK
//fromParts方法
//参数1:URI 的 scheme
//参数2:包路径
//参数3:
Uri uri = Uri.fromParts("package", "com.great.activity_intent", null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);
//安装APK(???)
Uri uri = Uri.fromParts("package", "com.great.activity_intent", null);
Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, uri);
startActivity(intent);
//调用搜索
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "android");
startActivity(intent);
多个Activity之间传值
可以通过Bundle对象存储需要传递的数据,例如:
在IntentDemoActivity里面传值,
Intent explicitIntent=new Intent(IntentDemoActivity.this, ExplicitActivity.class); //这是在Intent的构造函数中指定
EditText nameText=(EditText)findViewById(R.id.username);
// 通过Bundle对象存储需要传递的数据
Bundle bundle=new Bundle();
bundle.putString("userName", nameText.getText().toString());
//把Bundle对象bundle给explicitIntent
explicitIntent.putExtras(bundle);
startActivity(explicitIntent);
在ExplicitActivity获取值:
//获取Intent中的Bundle对象
Bundle bundle = this.getIntent().getExtras();
//获取Bundle中的数据,注意类型和key
String userName=bundle.getString("userName");
两个个Activity之间切换
在ExplicitActivity页面上加一个返回按钮,并在事件写如下代码:
/*给上一个Activity返回结果*/
Intent intent=new Intent(ExplicitActivity.this, IntentDemoActivity.class); //这是在Intent的构造函数中指定 ExplicitActivity.this.setResult(RESULT_OK,intent); /*结束本Activity*/ ExplicitActivity.this.finish();
这样就返回到IntentDemoActivity这个Activity去了。
通常,我们都会尽量使数据模型的变化尽量简单。但有些情况下,不得不进行大的改动,甚至是重新设计数据模型。在这种情况下,之前提过的简单数据迁移已经无法适应了,需要引入Mapping Model这个中间层。
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:NO], NSInferMappingModelAutomaticallyOption, nil];
把NSInferMappingModelAutomaticallyOption设置为NO后,我们需要手工指定映射模型: NSString *mappingModelPath = [[NSBundle mainBundle] pathForResource:@"mappingModel3to4" ofType:@"cdm"];
NSURL *mappingModelUrl = [NSURL fileURLWithPath:mappingModelPath];
NSMappingModel *mappingModel = [[[NSMappingModel alloc] initWithContentsOfURL:mappingModelUrl] autorelease];
接着,进行实质性的数据迁移。简单起见,这里就没有做错误检查了: NSMigrationManager *migrationManager = [[[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel] autorelease];
if (![migrationManager migrateStoreFromURL:storeURL type:NSSQLiteStoreType options:nil withMappingModel:mappingModel toDestinationURL:tmpStoreURL destinationType:NSSQLiteStoreType destinationOptions:nil error:&error]) {
NSLog(@"Error migrating %@, %@", error, [error userInfo]);
abort();
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *oldStoreName = @"cdNBA_old.sqlite";
NSURL *oldStoreURL = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:oldStoreName]];
[fileManager moveItemAtURL:storeURL toURL:oldStoreURL error:&error];
[fileManager moveItemAtURL:tmpStoreURL toURL:storeURL error:&error];
再跑一遍Demo,然后在终端里查看: