在這裏,我需要獲得電池的狀態與services.Service每秒鐘後獲取電池的狀態,當我斷開設備它顯示USB連接到設備多少時間?請建議解決方案...............使用服務獲取電池電量
public class serviceclass extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
是的你上面粘貼的代碼只是一個簡單的服務示例代碼。讓我們知道你有什麼進一步嘗試。 –
當我添加USB設備時,它會啓動服務,記下當前連接USB的時間,並記錄每秒鐘後的時間,當我移除USB時,它會顯示連接的時間。 – Shobin