http://www.2cto.com/kf/201201/117397.html
在Android可能有的系统信息没有直接提供API接口来访问,为了获取系统信息时我们就要在用shell指令来获取信息,这时我们可以在代码中来执行命令 ,这里主要用到ProcessBuilder 这个类.
代码部分 :
[java]
package com.yin.system_analysis;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private final static String[] ARGS = {"ls","-l"};
private final static String TAG = "com.yin.system";
Button mButton;
TextView myTextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.myButton);
myTextView = (TextView) findViewById(R.id.textView);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myTextView.setText(getResult());
}
});
}
public String getResult(){
ShellExecute cmdexe = new ShellExecute ( );
String result="";
try {
result = cmdexe.execute(ARGS, "/");
} catch (IOException e) {
Log.e(TAG, "IOException");
e.printStackTrace();
}
return result;
}
private class ShellExecute {
/*
* args[0] : shell 命令 如"ls" 或"ls -1";
* args[1] : 命令执行路径 如"/" ;
*/
public String execute ( String [] cmmand,String directory)
throws IOException {
String result = "" ;
try {
ProcessBuilder builder = new ProcessBuilder(cmmand);
if ( directory != null )
builder.directory ( new File ( directory ) ) ;
builder.redirectErrorStream (true) ;
Process process = builder.start ( ) ;
//得到命令执行后的结果
InputStream is = process.getInputStream ( ) ;
byte[] buffer = new byte[1024] ;
while ( is.read(buffer) != -1 ) {
result = result + new String (buffer) ;
}
is.close ( ) ;
} catch ( Exception e ) {
e.printStackTrace ( ) ;
}
return result ;
}
}
}
目前版本的Titanium的一个弱点就是不能画图!这回我们说说通过flot在Titanium中画图表。
flot的下载
flot的主页:http://code.google.com/p/flot/
下载flot-0.7.zip后,把以下文件拷贝到自己的工程中:
- jquery.flot.js
- jquery.js
flot的测试
作成以下flot测试用的plot_window.js:
var win = Ti.UI.currentWindow;
var webView = Ti.UI.createWebView({
url: 'plot.html'
});
win.add(webView);
其中WebView中表示的plot.html文件如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Flot Examples</title>
<link href="/blog_article/layout.css" rel="stylesheet" type="text/css">
<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="/excanvas.min.js"></script><![endif]-->
<script language="javascript" type="text/javascript" src="/blog_article/jquery.js"></script>
<script language="javascript" type="text/javascript" src="/blog_article/jquery.flot.js"></script>
</head>
<body>
<h1>Data Graph</h1>
<div id="graph" ></div>
<script type="text/javascript">
var weights = [[1301270400000,0],[1301875200000,8.25],[1302480000000,22],[1303084800000,29],[1303689600000,36.5]];
var ticks = [1301270400000,1301875200000,1302480000000,1303084800000,1303689600000];
var setting = {
series: {
lines: { show:true},
points: { show:true}
},
xaxis: {
mode:"time",
timeformat:"%m/%d",
tickSize: [7,"day"],
ticks: ticks
},
yaxis: {
ticks: 10,
min: 0,
},
grid: {
backgroundColor: { colors: ["#fff","#eee"] },
}
};
$.plot($("#graph"),[{data: weights, color: 2}], setting);
</script>
</body>
</html>
运行之后看看效果图:
在TableView中表示图表
var graphButton = Ti.UI.createButton({title: 'Graph'});
graphButton.addEventListener(
'click', function () {
if (records.length > 0) {
var weights = "[";
var ticks = "[";
for (i = records.length-1; i >= 0; i--) {
weights = weights + "[" + records[i].at.getTime() +","+records[i].weight+"],";
ticks = ticks + records[i].at.getTime() + ",";
}
weights = weights + "]"; ticks = ticks + "]";
var graphWindow = Ti.UI.createWindow({
url: 'plot_window.js',
weights: weights,
ticks: ticks
}
);
Ti.UI.currentTab.open(graphWindow);
}
}
);
win.leftNavButton = graphButton;
修改plot_window.js
webView.addEventListener('load', function(){
webView.evalJS('weights =' + win.weights + ';');
webView.evalJS('ticks =' + win.ticks + ';');
webView.evalJS('setting.xaxis.ticks = ticks;');
webView.evalJS('$.plot($("#graph"),[{data: weights, color: 2}], setting);');
});
运行之后,按下TableView左上角的Graph按钮后,看看效果图:
看看上面的网址,应该是你想要的
看看上面的网址,应该是你想要的
30美元,太贵了,哈哈!
Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,为了让大家更容易理解我还是照常写了一个简单的Demo。
工程结构图:
[img]
[/img]
Book:
package com.tutor.objecttran;
import android.os.Parcel;
import android.os.Parcelable;
public class Book implements Parcelable {
private String bookName;
private String author;
private int publishTime;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPublishTime() {
return publishTime;
}
public void setPublishTime(int publishTime) {
this.publishTime = publishTime;
}
public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
public Book createFromParcel(Parcel source) {
Book mBook = new Book();
mBook.bookName = source.readString();
mBook.author = source.readString();
mBook.publishTime = source.readInt();
return mBook;
}
public Book[] newArray(int size) {
return new Book[size];
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(bookName);
parcel.writeString(author);
parcel.writeInt(publishTime);
}
}
Person:
package com.tutor.objecttran;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = -7060210544600464481L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
ObjectTranDemo:
package com.tutor.objecttran;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ObjectTranDemo extends Activity implements OnClickListener {
private Button sButton,pButton;
public final static String SER_KEY = "com.tutor.objecttran.ser";
public final static String PAR_KEY = "com.tutor.objecttran.par";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
//我的一贯作风呵呵
public void setupViews(){
sButton = (Button)findViewById(R.id.button1);
pButton = (Button)findViewById(R.id.button2);
sButton.setOnClickListener(this);
pButton.setOnClickListener(this);
}
//Serializeable传递对象的方法
public void SerializeMethod(){
Person mPerson = new Person();
mPerson.setName("frankie");
mPerson.setAge(25);
Intent mIntent = new Intent(this,ObjectTranDemo1.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable(SER_KEY,mPerson);
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
//Pacelable传递对象方法
public void PacelableMethod(){
Book mBook = new Book();
mBook.setBookName("Android Tutor");
mBook.setAuthor("Frankie");
mBook.setPublishTime(2010);
Intent mIntent = new Intent(this,ObjectTranDemo2.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(PAR_KEY, mBook);
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
//铵钮点击事件响应
public void onClick(View v) {
if(v == sButton){
SerializeMethod();
}else{
PacelableMethod();
}
}
}ObjectTranDemo1:
package com.tutor.objecttran;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ObjectTranDemo1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView mTextView = new TextView(this);
Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);
mTextView.setText("You name is: " + mPerson.getName() + "/n"+
"You age is: " + mPerson.getAge());
setContentView(mTextView);
}
}ObjectTranDemo2:
package com.tutor.objecttran;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ObjectTranDemo2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView mTextView = new TextView(this);
Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);
mTextView.setText("Book name is: " + mBook.getBookName()+"/n"+
"Author is: " + mBook.getAuthor() + "/n" +
"PublishTime is: " + mBook.getPublishTime());
setContentView(mTextView);
}
}res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr wei's blog."
/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Serializable"
/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Parcelable"
/>
</LinearLayout>
AndroidManifest.xml文件(将两个新增的Activity,ObjectTranDemo1,ObjectTranDemo2)申明一下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutor.objecttran"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ObjectTranDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ObjectTranDemo1"></activity>
<activity android:name=".ObjectTranDemo2"></activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>