/proc/meminfo からデータを取得するサービスを作成しました。サービス クラスは次のとおりです。
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Vector;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MonitorService extends Service{
Vector<String> memFree, buffers, cached, active, inactive, swapTotal,dirty;
int TOTAL_INTERVALS = 440;
private BufferedReader readStream;
private String x;
int memTotal;
private Runnable readRunnable = new Runnable() {
public void run() {
read(); // We call here read() because to draw the graphic we need
// at less 2 read values.
while (readThread == Thread.currentThread()) {
try {
read();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
private Thread readThread = new Thread(readRunnable, "readThread");
class MyServiceDataBinder extends Binder {
MonitorService getService() {
return MonitorService.this;
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
memFree = new Vector<String>(this.TOTAL_INTERVALS);
buffers = new Vector<String>(this.TOTAL_INTERVALS);
cached = new Vector<String>(this.TOTAL_INTERVALS);
active = new Vector<String>(this.TOTAL_INTERVALS);
inactive = new Vector<String>(this.TOTAL_INTERVALS);
swapTotal = new Vector<String>(this.TOTAL_INTERVALS);
dirty = new Vector<String>(this.TOTAL_INTERVALS);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyServiceDataBinder();
}
private void read() {
try {
readStream = new BufferedReader(new FileReader("/proc/meminfo"));
x = readStream.readLine();
while (x != null) {
/*
* When the limit TOTAL_INTERVALS is surpassed by some vector we
* have to remove all the surpassed elements because, if not,
* the capacity of the vector will be increase x2.
*/
while (memFree.size() >= this.TOTAL_INTERVALS)
memFree.remove(memFree.size() - 1);
while (buffers.size() >= this.TOTAL_INTERVALS)
buffers.remove(buffers.size() - 1);
while (cached.size() >= this.TOTAL_INTERVALS)
cached.remove(cached.size() - 1);
while (active.size() >= this.TOTAL_INTERVALS)
active.remove(active.size() - 1);
while (inactive.size() >= this.TOTAL_INTERVALS)
inactive.remove(inactive.size() - 1);
while (swapTotal.size() >= this.TOTAL_INTERVALS)
swapTotal.remove(swapTotal.size() - 1);
while (dirty.size() >= this.TOTAL_INTERVALS)
dirty.remove(dirty.size() - 1);
// We read the memory values. The percents are calculated in the
// AnotherMonitor class.
if (x.startsWith("MemTotal:"))
memTotal = Integer.parseInt(x.split("[ ]+", 3)[1]);
if (x.startsWith("MemFree:"))
memFree.add(0, x.split("[ ]+", 3)[1]);
if (x.startsWith("Buffers:"))
buffers.add(0, x.split("[ ]+", 3)[1]);
if (x.startsWith("Cached:"))
cached.add(0, x.split("[ ]+", 3)[1]);
if (x.startsWith("Active:"))
active.add(0, x.split("[ ]+", 3)[1]);
if (x.startsWith("Inactive:"))
inactive.add(0, x.split("[ ]+", 3)[1]);
if (x.startsWith("SwapTotal:"))
swapTotal.add(0, x.split("[ ]+", 3)[1]);
if (x.startsWith("Dirty:"))
dirty.add(0, x.split("[ ]+", 3)[1]);
x = readStream.readLine();
}
readStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
作成したサービスを呼び出すクラス:
import java.text.DecimalFormat;
import java.util.Vector;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.widget.TextView;
import com.sliit.droidman.R;
public class Processor extends Activity{
private TextView ram1, ram2, ram3, ram4, ram5, ram6, ram7;
private MonitorService MyMonitor;
private DecimalFormat myFormat = new DecimalFormat("##,###,##0");
private Handler myHandler;
private Runnable drawRunnable = new Runnable() {
public void run() {
setTextLabel(ram1,MyMonitor.memFree);
setTextLabel(ram2,MyMonitor.buffers);
setTextLabel(ram3,MyMonitor.cached);
setTextLabel(ram4,MyMonitor.active);
setTextLabel(ram5,MyMonitor.inactive);
setTextLabel(ram6,MyMonitor.swapTotal);
setTextLabel(ram7,MyMonitor.dirty);
myHandler.postDelayed(this, 1000);
}
};
private ServiceConnection MyService = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
MyMonitor = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
MyMonitor = ((MonitorService.MyServiceDataBinder)service).getService();
ram1.setText(myFormat.format(MyMonitor.memTotal) + " kB");
myHandler.post(drawRunnable); <--- A null pointer Exceprtion occurs
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
startService(new Intent(this, MonitorService.class));
setContentView(R.layout.resources_processor);
ram1 = (TextView) findViewById(R.id.Appres_ProcessorInfo1);
ram2 = (TextView) findViewById(R.id.Appres_ProcessorInfo2);
ram3 = (TextView) findViewById(R.id.Appres_ProcessorInfo3);
ram4 = (TextView) findViewById(R.id.Appres_ProcessorInfo4);
ram5 = (TextView) findViewById(R.id.Appres_ProcessorInfo5);
ram6 = (TextView) findViewById(R.id.Appres_ProcessorInfo6);
ram7 = (TextView) findViewById(R.id.Appres_ProcessorInfo7);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
bindService(new Intent(this, MonitorService.class), MyService, 0);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
unbindService(MyService);
}
private void setTextLabel(TextView textView, Vector<String> y) {
if (!y.isEmpty()) {
textView.setText(myFormat.format(Integer.parseInt(y.firstElement())) + " kB");
//textViewP.setText(myFormatPercent.format(Integer.parseInt(y.firstElement())* 100 / (float) myAnReaderService.memTotal) + "%");
} else
textView.setText("Value");
}
}
ログ猫の出力:
07-13 10:20:08.109: E/AndroidRuntime(745): FATAL EXCEPTION: main
07-13 10:20:08.109: E/AndroidRuntime(745): java.lang.NullPointerException
07-13 10:20:08.109: E/AndroidRuntime(745): at com.sliit.droidman.systemresources.Processor$2.onServiceConnected(Processor.java:49)
07-13 10:20:08.109: E/AndroidRuntime(745): at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1097)
07-13 10:20:08.109: E/AndroidRuntime(745): at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1114)
07-13 10:20:08.109: E/AndroidRuntime(745): at android.os.Handler.handleCallback(Handler.java:615)
07-13 10:20:08.109: E/AndroidRuntime(745): at android.os.Handler.dispatchMessage(Handler.java:92)
07-13 10:20:08.109: E/AndroidRuntime(745): at android.os.Looper.loop(Looper.java:137)
07-13 10:20:08.109: E/AndroidRuntime(745): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-13 10:20:08.109: E/AndroidRuntime(745): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 10:20:08.109: E/AndroidRuntime(745): at java.lang.reflect.Method.invoke(Method.java:511)
07-13 10:20:08.109: E/AndroidRuntime(745): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-13 10:20:08.109: E/AndroidRuntime(745): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-13 10:20:08.109: E/AndroidRuntime(745): at dalvik.system.NativeStart.main(Native Method)
07-13 10:20:15.860: E/Trace(764): error opening trace file: No such file or directory (2)
コードで指摘したコードからヌル ポインター例外が発生します。このセクションで私が行うエラーを理解するのを手伝ってください。
ありがとうございました。