2013-03-28 35 views
0

我對Android開發很陌生,所以希望這是一個很容易回答的問題。我做了很多搜索,但找不到答案 - 可能我正在以錯誤的方式去解決。來自一個佈局上顯示的兩個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理解是目前新手。

非常感謝

+1

不要在一個活動中都只有東西... –

回答

0

使用靜態方法,例如

static String getDeviceID(TelephonyManager phonyManager) 

然後調用與類名的方法,

classB.getDeviceID(phonyManager);

0

我想你可能在這裏有一些基本的概念錯誤。 Activity僅適用於UI。不需要您爲一個特定功能需要一個Activity。因此,您可以在一項活動中同時具備這兩項功能,並更新其佈局。

0

你可以寫這些值代入Sharedpreferences ....

getSharedPreferences("sp", MODE_PRIVATE). edit(). putString("login", ed_login.getText().toString()). commit();

,那麼你可以在任何地方通過得到它...

getSharedPreferences("sp", MODE_PRIVATE).getString("login", ""); 
相關問題