sudo 环境变量设置

编辑 /etc/sudoers :

Defaults   env_reset
Defaults   secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

修改为:

#不重置sudo的环境变量,沿用当前用户的环境变量
Defaults    !env_reset
#Defaults   secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Android Framework 重启系统代码

private void RebootIntoRecovery() {
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        pm.reboot("recovery");
}
<uses-permission android:name="android.permission.REBOOT" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.android.settings"
        coreApp="true"
        android:sharedUserId="android.uid.system">

使用FileChannel拷贝文件

使用FileChannel拷贝文件的效率比逐字节拷贝要更高效,但是FileChannel有一个限制:操作的文件大小不能大于2GB

public void copy(File src, File dst) throws IOException {
    FileInputStream inStream = new FileInputStream(src);
    FileOutputStream outStream = new FileOutputStream(dst);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();
    outStream.close();
}

Objc中的对象归档

#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSKeyedArchiver.h>

@interface Foo : NSObject<NSCoding> {
    NSString *strVal;
    int intVal;
    float floatVal;
}
@property(copy, nonatomic)NSString *strVal;
@property int intVal;
@property float floatVal;
@end


@implementation Foo
@synthesize strVal, intVal, floatVal;

-(void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:strVal forKey:@"FoostrVal"];
    [encoder encodeInt:intVal forKey:@"FoointVal"];
    [encoder encodeFloat:floatVal forKey:@"FoofloatVal"];
}

-(id)initWithCoder:(NSCoder *)decoder {
    strVal = [decoder decodeObjectForKey:@"FoostrVal"];
    intVal = [decoder decodeIntForKey:@"FoointVal"];
    floatVal = [decoder decodeFloatForKey:@"FoofloatVal"];
    return self;
}
@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Foo *myFoo1 = [[Foo alloc]init];
        Foo *myFoo2;
        [myFoo1 setStrVal:@"This is the string"];
        [myFoo1 setIntVal:12345];
        [myFoo1 setFloatVal:98.6];
        [NSKeyedArchiver archiveRootObject:myFoo1 toFile:@"foo.arch"];
        myFoo2 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"foo.arch"];
        NSLog(@"%@ %i %g\n", [myFoo2 strVal], [myFoo2 intVal], [myFoo2 floatVal]);
    }
    return 0;
}

获取 Android 源码包中的资源

 String path = "com/example/testres/1.jpg";
        InputStream is = getClassLoader().getResourceAsStream(path);
        Drawable background = Drawable.createFromStream(is, null);