2015
06.30
启动:
memcached -d -u user -p 11211
默认内存64M,如果想指定内存大小,使用-m,后面加上要分配的内存大小(单位M)
停止:
service memcached stop
清空缓存:
telnet 127.0.0.1 11211
flush_all 回车
quit
查看状态:
telnet 127.0.0.1 11211
stats
其中,STAT bytes 一行显示的是当前占用内存的大小,
STAT limit_maxbytes 一行显示的是可用的最大内存。
2015
06.29
package com.example.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class MainActivity extends ActionBarActivity implements MqttCallback{
private MqttClient mqttClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void clickButton(View button){
switch (button.getId()){
case R.id.register:
new Thread(new Runnable() {
@Override
public void run() {
int qos = 2;
String broker = "tcp://iot.eclipse.org:1883";
EditText editTextID = (EditText)findViewById(R.id.id);
String clientId = editTextID.getText().toString();
MemoryPersistence persistence = new MemoryPersistence();
try{
mqttClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
mqttClient.connect(connOpts);
mqttClient.subscribe(clientId);
mqttClient.setCallback(MainActivity.this);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), mqttClient.isConnected() ? "连接成功" : "连接失败", Toast.LENGTH_LONG).show();
}
});
}
catch (Exception e){
e.printStackTrace();
}
}
}).start();
break;
case R.id.send:
EditText editTextTargetID =(EditText)findViewById(R.id.targetID);
final String targetID = editTextTargetID.getText().toString();
String content = ((EditText)findViewById(R.id.content)).getText().toString();
final MqttMessage message = new MqttMessage(content.getBytes());
if(mqttClient==null || !mqttClient.isConnected()){
Toast.makeText(getApplicationContext(),"MQTT未连接",Toast.LENGTH_LONG).show();
return;
}
new Thread(new Runnable() {
@Override
public void run() {
try{
mqttClient.publish(targetID, message);
}
catch (Exception e){
e.printStackTrace();
}
}
}).start();
break;
}
}
@Override
public void connectionLost(final Throwable cause) {
this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "失去连接:" + cause.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
@Override
public void messageArrived(String topic, MqttMessage message)
throws Exception {
final String msg = new String(message.getPayload());
this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "收到:"+msg, Toast.LENGTH_LONG).show();
}
});
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
try {
final String msg = new String(token.getMessage().getPayload());
this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "发送完毕:" + msg, Toast.LENGTH_LONG).show();
}
});
}
catch (Exception e){
e.printStackTrace();
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="50dp"
android:layout_height="fill_parent"
android:gravity="center"
android:text="ID:"/>
<EditText
android:id="@+id/id"
android:layout_width="100dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="register"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="clickButton" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="80dp"
android:layout_height="fill_parent"
android:gravity="center"
android:text="接收方ID:"/>
<EditText
android:id="@+id/targetID"
android:layout_width="50dp"
android:layout_height="fill_parent" />
<TextView
android:layout_width="40dp"
android:layout_height="fill_parent"
android:gravity="center"
android:text="内容:"/>
<EditText
android:id="@+id/content"
android:layout_width="100dp"
android:layout_height="fill_parent" />
<Button
android:text="Send"
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="clickButton" />
</LinearLayout>
</LinearLayout>
2015
06.22
Service:运行于主线程,所以在运行过程中会造成UI没有响应;必须要调用stopService()或者stopSelf()方法去停止。
IntentService:会创建一个独立的新线程,所以不会影响UI;执行完成后自动停止,不必调用stopService()或stopSelft()方法。