链接数据库

1
2
3
4
5
6
7
8
9
10
var MongoClient = require('mongodb').MongoClient,
assert = require('assert'); // 断言

var url = 'mongodb://localhost:27017/myproject'
MongoClient.connect(url,function(err,db)){
assert.equal(null,err); // 如果err == null 继续执行
console.log('Connected correctly to server');

db.close();
}

插入文档

1
2
3
4
5
6
7
8
9
10
var insertDocument = function(db,callback) {
var collection = db.collection('document');
collection.insert([{a:1},{a:2},{a:3}],function(err,result){
assert.equal(err, null);
assert.equal(3, result.result.n);
assert.equal(3, result.ops.length);
console.log("Inserted 3 documents into the document collection");
callback(result);
});
}

可以在连接时增加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");

insertDocuments(db, function() {
db.close();
});
});

修改文档

1
2
3
4
5
6
7
8
9
10
11
12
var updateDocument = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Update document where a is 2, set b equal to 1
collection.update({ a : 2 }
, { $set: { b : 1 } }, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log("Updated the document with the field a equal to 2");
callback(result);
});
}

也可以在连接时回调

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");

insertDocuments(db, function() {
updateDocument(db, function() {
db.close();
});
});
});

删除

1
2
3
4
5
6
7
8
9
10
11
var removeDocument = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Insert some documents
collection.remove({ a : 3 }, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log("Removed the document with the field a equal to 3");
callback(result);
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");

insertDocuments(db, function() {
updateDocument(db, function() {
removeDocument(db, function() {
db.close();
});
});
});
});

查询

1
2
3
4
5
6
7
8
9
10
11
12
var findDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Find some documents
collection.find({}).toArray(function(err, docs) {
assert.equal(err, null);
assert.equal(2, docs.length);
console.log("Found the following records");
console.dir(docs)
callback(docs);
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");

insertDocuments(db, function() {
updateDocument(db, function() {
removeDocument(db, function() {
findDocuments(db, function() {
db.close();
});
});
});
});
});

Android代码

  1. 获取手机分辨率
1
2
3
4
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
  1. dp转px
1
2
3
4
public static int dip2px(Context context,float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (dpValue *scale + 0.5f);
}
  1. px转dp
1
2
3
4
public static int px2dip(Context context,float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f) - 15;
}
  1. 不需要Context得到density的方法
1
Resources.getSystem().getDisplatMetrics().density;

EventBus是一个非常流行的开源库,用于Android事件发布/订阅,通过解耦发布者和订阅者简化Android事件传递。事件传递可以用于Android四大组件也可以作用于异步线程和主线程的通讯。
相比HandlerBroadCastReceiverInterface回调,EventBus的优点代码更简洁,使用简单将事件发布和订阅充分解耦。

Read More

再好的应用,也需要有人去发现他,我只是有那么一些途径,让我更快的找到它们而已。有网页,有微博,也有微信公众号,希望我的这些推荐可以让你更好的发现那些优秀、精美的应用。

Read More

  1. 将Android手机的调试模式打开
  2. 按住选择Mac的option键,选择Apple图标选项的系统信息
  3. 找到USB,查看手机的厂商ID,比如我的MX Pro是0x2a45
  4. 在终端中输入并执行echo 0x2a45 >> ~/.android/adb_usb.ini
  5. 重启ADB服务(adb restart或者重启Eclipse、AndroidStudio)