这是主要活动,我称之为S级(我在哪里获取设备信息和ram
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt=(TextView) findViewById(R.id.txt);
S s= new S();
long value=s.mem();
txt.setText("sa"+value);
}
}
public class S extends Service
{
public long mem()
{
long value=0;
int id = android.os.Process.myPid();
ActivityManager activityManager=(ActivityManager)getSystemService(ACTIVITY_SERVICE);
MemoryInfo outInfo=activityManager.getProcessMemoryInfo(new int[]{id})[0];
value=outInfo.getTotalPrivateDirty();
String manufacturer = Build.MANUFACTURER;
String brand = Build.BRAND;
String product = Build.PRODUCT;
String model = Build.MODEL;
String a = getResources().getString(R.string.app_name);
return value+manufacturer+brand+product+model+applicationName;
}
@Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
return new ServiceReaderDataBinder();
}
class ServiceReaderDataBinder extends Binder
{
S getService(){ return S.this;}
}
}
答案 0 :(得分:1)
您正在创建一个服务实例,但实际上您应该绑定该服务。修改您的活动,如下所示
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent(this, s.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
public ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
ServiceReaderDataBinder binder=(ServiceReaderDataBinder )service;
s=binder.getService();
((TextView)findViewById(R.id.txt)).setText(s.mem());
}
};
注意:别忘了在清单文件中声明您的服务