我对 Android 开发非常陌生,所以希望这是一个容易回答的问题。我做了很多搜索,但找不到答案 - 可能我走错了路。
我有两个类文件,一个是获取电池电量信息(下面的 A 类),另一个使用 TelephonyManager 获取设备 IMEI 并显示它(下面的 B 类)。
我无法弄清楚如何让这两个类的值出现在同一布局上。
这是A类:
package com.XXXXXX.XXXXXX;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class AboutScreen extends ListActivity {
/** Called when the activity is first created. */
private String[] strText = new String[] {"Battery Level", "Voltage", "Status"};
private int voltage = 0;
private boolean trun = true;
private Handler myHandler = new Handler();
private Runnable myRun = new Runnable() {
public void run() {
updateNow();
}
};
// using Thread to keep the process running
private Thread myThread = new Thread() {
public void run () {
do {
batteryLevelUpdate();
myHandler.post(myRun);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (trun);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(getApplicationContext(), "email", 2000).show();
startMonitor();
}
@Override
public void onDestroy() {
trun = false;
super.onDestroy();
}
private void startMonitor() {
myThread.start();
}
private void updateNow() {
ListView thisListView = getListView();
thisListView.setEnabled(false);
ArrayAdapter<String> myList = new ArrayAdapter<String>(this, R.layout.about_screen, strText);
thisListView.setAdapter(myList);
}
private void batteryLevelUpdate() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
int onplug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
boolean onUSB = onplug == BatteryManager.BATTERY_PLUGGED_USB;
boolean onAC = onplug == BatteryManager.BATTERY_PLUGGED_AC;
String strStatus = "Charging on ";
if (isCharging && onUSB)
strStatus += "USB";
else if (isCharging && onAC)
strStatus += "AC Power";
else
strStatus = "Battery Discharging";
strText[0] = "Battery Level: " + Integer.toString(level) + "%";
strText[1] = "Voltage: " + Integer.toString(voltage) + "mV";
strText[2] = strStatus;
//strText[3] = "test";
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
}
这是B类:
package com.XXXXXX.XXXXXX;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;
public class AndroidTelephonyManager extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phone);
TextView textDeviceID = (TextView)findViewById(R.id.deviceid);
//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
textDeviceID.setText(getDeviceID(telephonyManager));
}
String getDeviceID(TelephonyManager phonyManager){
String id = phonyManager.getDeviceId();
if (id == null){
id = "not available";
}
int phoneType = phonyManager.getPhoneType();
switch(phoneType){
case TelephonyManager.PHONE_TYPE_NONE:
return "NONE: " + id;
case TelephonyManager.PHONE_TYPE_GSM:
return "GSM: IMEI=" + id;
case TelephonyManager.PHONE_TYPE_CDMA:
return "CDMA: MEID/ESN=" + id;
/*
* for API Level 11 or above
* case TelephonyManager.PHONE_TYPE_SIP:
* return "SIP";
*/
default:
return "UNKNOWN: ID=" + id;
}
}
}
这是显示电池电量信息的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="12sp"
android:clickable="false" />
但是我不知道如何从 B 类中获取 IMEI 信息以显示。理想情况下,它将是电池电量项目下方的另一个列表行。任何关于如何将不同活动组合到一个布局中的建议都会非常有用。
如果我的解释不是很好,我深表歉意,但我对 Java 的理解目前是新手。
非常感谢