当前位置: 编程技术>移动开发
本页文章导读:
▪小弟我这段时间暂时停止更新博客 我这段时间暂时停止更新博客。
如题。
......
▪ PhoneState查看拨号器状态及展示联系人信息 PhoneState查看拨号器状态及显示联系人信息
添加权限
<uses-permission
android:name="android.permission.READ_PHONE_STATE" />
private TextView myTextView1;
public void onCreate(Bundle savedInstanceState) {
super.onC.........
▪ 运用定制的NSMutableDictionary方法对NSMutableArray排序 使用定制的NSMutableDictionary方法对NSMutableArray排序
首先,给NSMutableDictionary建一个分类,里面加上自定义的排序方法。
头文件:
#import <Foundation/Foundation.h>
@interface NSMutableDictionary(NSMutab.........
[1]小弟我这段时间暂时停止更新博客
来源: 互联网 发布时间: 2014-02-18
我这段时间暂时停止更新博客。
如题。
[2] PhoneState查看拨号器状态及展示联系人信息
来源: 互联网 发布时间: 2014-02-18
PhoneState查看拨号器状态及显示联系人信息
添加权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
private TextView myTextView1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phone_state);
myTextView1 = (TextView) findViewById(R.id.myTextView1);
/* 新增的PhoneStateListener */
MyPhoneCallListener myPhoneCallListener = new MyPhoneCallListener();
/* 取得电话服务 */
TelephonyManager tm = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
/* 注册Listener */
tm.listen(myPhoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
}
/* 内部class继承PhoneStateListener */
public class MyPhoneCallListener extends PhoneStateListener {
/* 重写onCallStateChanged当状态改变时改变myTextView1的文字及颜色 */
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
/* 无任务状态时 */
case TelephonyManager.CALL_STATE_IDLE:
myTextView1.setTextColor(Color.RED);
myTextView1.setText("无任何状态");
break;
/* 接起电话时 */
case TelephonyManager.CALL_STATE_OFFHOOK:
myTextView1.setTextColor(Color.BLUE);
myTextView1.setText("接起电话时");
break;
/* 电话进来时 */
case TelephonyManager.CALL_STATE_RINGING:
getContactPeople(incomingNumber);
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
private void getContactPeople(String incomingNumber) {
myTextView1.setTextColor(Color.BLUE);
ContentResolver contentResolver = getContentResolver();
Cursor cursor = null;
/* cursor里要放的字段名称 */
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
/* 用来电电话号码查找该联系人 */
cursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
ContactsContract.CommonDataKinds.Phone.NUMBER + "=?",
new String[] { incomingNumber }, "");
/* 找不到联系人 */
if (cursor.getCount() == 0) {
myTextView1.setText("未知联系人:" + incomingNumber);
} else if (cursor.getCount() > 0) {
cursor.moveToFirst();
/* projection这个数组里 */
String name = cursor.getString(1);
myTextView1.setText(name + ":" + incomingNumber);
}
}
[3] 运用定制的NSMutableDictionary方法对NSMutableArray排序
来源: 互联网 发布时间: 2014-02-18
使用定制的NSMutableDictionary方法对NSMutableArray排序
首先,给NSMutableDictionary建一个分类,里面加上自定义的排序方法。
头文件:
#import <Foundation/Foundation.h> @interface NSMutableDictionary(NSMutableDictionaryCompare) - (NSComparisonResult)compareWithDictionary:(NSMutableDictionary *) dic; @end
实现文件:
#import "CustomCompare.h"
@implementation NSMutableDictionary(NSMutableDictionaryCompare)
- (NSComparisonResult)compareWithDictionary:(NSMutableDictionary *) dic{
NSMutableDictionary *dic1 = self;
NSString *name1 = [dic1 objectForKey:@"Name"];
NSString *name2 = [dic objectForKey:@"Name"];
return [name2 compare:name1];
}
@end
示例:
NSMutableDictionary *dic1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Michael", @"Name", @"26", @"Age", nil]; NSMutableDictionary *dic2 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Eric", @"Name", @"24", @"Age", nil]; NSMutableDictionary *dic3 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Emilla", @"Name", @"25", @"Age", nil]; NSMutableDictionary *dic4 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Josh", @"Name", @"32", @"Age", nil]; NSMutableDictionary *dic5 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Echo", @"Name", @"22", @"Age", nil]; NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:dic1, dic2, dic3, dic4, dic5, nil]; [dic1 release]; [dic2 release]; [dic3 release]; [dic4 release]; [dic5 release]; [array sortUsingSelector:@selector(compareWithDictionary:)]; NSLog(@"%@", array); [array release];
示例输出:
{
Age = 26;
Name = Michael;
},
{
Age = 32;
Name = Josh;
},
{
Age = 24;
Name = Eric;
},
{
Age = 25;
Name = Emilla;
},
{
Age = 22;
Name = Echo;
}
最新技术文章: