2

アンドロイドアプリを実行した後にバッテリー残量を調べようとしています。電話機が完全に充電されると、コードは常に100%の電池残量を返します。 75%のバッテリーなどでアプリを起動すると、テスト終了時に実際のバッテリー残量が返されます。私は自分のコンピュータにプラグインされた電話で私のテストを始める。理想的には、開始%と終了%を得ることができるようにしたいと思いますが、その両方で100%を読みます。だからここに私がやっているものです...私は私の活動からこれを呼び出す:インテントとBroadcastReceiverを使用してバッテリー残量を取得する際の奇妙な動作

this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 

その後BroadcastReceiverで、私は(つまり、バッテリーレベルの変更ので、時間を引き継ぐ)原料の束をします。次にBoradcastReceiverの最後に私はこれを呼び出します

int level2 = intent.getIntExtra("level", 0); /* Ending battery level */ 
String end = "Ending battery level: " + String.valueOf(level2) + "%"; 

そして、文字列の最後をサーバーに送ります。ここで

は私のコードは次のようになります。

package com.mdog.datareceive; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.os.PowerManager; 
import android.util.Log; 
import android.widget.TextView; 


public class Receive extends Activity { 

    /* Test parameters */ 
    static int BYTES_TO_READ = 1024*1024*50; /* The size of the data for each transfer */ 
    static int TIMES_TO_READ = 20;    /* The number of times the client will request the data */ 
    static int PLAYER_BUFFER_SIZE = 1638400; /* Clients buffer size 1638400 = 1.57MB */ 
    static int SLEEP_TIME_MS = 5000; 

    /* Display Info */ 
    String start = "empty";     /* String holder for the starting battery life */ 
    String end = "empty2";     /* String holder for the ending battery life */ 
    int allMBytes = 0;      /* Integer holder for the total number of megabytes received during the test */ 
    TextView tv;       /* The view the phone displays after completion of test */ 

    /* Server Info */ 
    String serverIP = "192.168.0.104";  /* Server IP */ 
    int serverPort = 11313;     /* Server port number */ 



    private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){ 

     @Override 
     public void onReceive(Context arg0, Intent intent) { 

      //int level = intent.getIntExtra("level", 0); 

      Socket connectionSocket = null;       /* Socket to communicate with server */ 
      byte[] inputHolderByteArray = new byte[PLAYER_BUFFER_SIZE]; /* Used to read from the socket */ 

      int bufferBytes = 0;  /* filling the second buffer */ 
      int totalBytesRead = 0;  /* The total number of bytes read for this transfer round */ 
      int read=0;     /* reading from stream, will contain the number of bytes read or -1 if the end has been hit */ 

      /* Acquire a wake lock for the phone so it does not go to sleep */ 
      PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
      PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName()); 

      /* Connect to Server */ 
      try { 

       connectionSocket = new Socket(serverIP, serverPort); 
       connectionSocket.setReceiveBufferSize(PLAYER_BUFFER_SIZE); //1.5ish MB 
       connectionSocket.setKeepAlive(true); 
       PrintWriter out = new PrintWriter(connectionSocket.getOutputStream(), true); 

       out.print("Client Info: Bytes expected per transfer:" + BYTES_TO_READ + ", Number of transfer to request:" + TIMES_TO_READ+ ", Buffer size:" +PLAYER_BUFFER_SIZE+ ", Sleep time(ms):" + SLEEP_TIME_MS + "\n"); 
       out.flush(); 

       wl.acquire(); 

       for(int i=0; i<TIMES_TO_READ; i++){ 

        out.print("Start\n"); 
        out.flush(); 

        while(totalBytesRead < BYTES_TO_READ){ 

         /* Read at most PLAYER_BUFFER_SIZE bytes */ 
         read = connectionSocket.getInputStream().read(inputHolderByteArray, 0, PLAYER_BUFFER_SIZE); 

         if(read != -1){ 
          bufferBytes += read; 
          totalBytesRead += read; 
          if(bufferBytes >= PLAYER_BUFFER_SIZE){ 
            try { 
             Thread.sleep(SLEEP_TIME_MS); 
            } catch (InterruptedException e) { 
             e.printStackTrace(); 
            } 
           bufferBytes = 0; 
          } 
         } 
         else{ 
          /* End of stream reached */ 
          break; 
         }  
        } 

        allMBytes += ((totalBytesRead/1024)/1024); 
        totalBytesRead = 0; 

        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 

       } 

       int level2 = intent.getIntExtra("level", 0); /* Ending battery level */ 

       /* Put data on screen */ 
       start = "Starting battery level: 100%"; 
       end = "Ending battery level: " + String.valueOf(level2) + "%"; 
       out.print("Test Completed: " + start + ", " + end + ", Megabytes Read: " + allMBytes + "\n"); 
       out.flush(); 

       wl.release(); 

       tv.setText(start + " \n" + end + " \n Total MB transferred:" + allMBytes); 
       setContentView(tv); 

      } catch (UnknownHostException e) { 
       Log.i("UnknownHost exception ", " ******************** Log Msg UHE " + e.getLocalizedMessage()); 
       e.printStackTrace(); 
      } catch (IOException e2) { 
       Log.i("IO exception ", "******************** Log Msg IOE " + e2.toString()); 
       e2.printStackTrace(); 
      } 
     } 
    }; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     tv = new TextView(this);    /* The view to post text to */ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 
    } 
} 
+0

一般的な注意:レベルの最大値を含む余分なEXTRA_SCALE( "スケール")があります。最大値は100と思われますので、パーセント値についてのあなたの仮定は正しいですが、これは常にそうであると私は考えません。 – sstn

答えて

1

まず、intent.getIntExtra("level", 0);はパーセンテージではありません。パーセンテージを取得するには、intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 1)intent.getIntExtra(BatteryManager.EXTRA_SCALE, 1)と比較する必要があります。

onReceive()BroadcastReceiverなどのメインアプリケーションスレッドでネットワークI/Oを実行します。

第3に、バッテリーレベルを自分と比較しています。 次のACTION_BATTERY_CHANGEDのブロードキャストを待って新しいバッテリレベルを調べる必要があります。

+0

@CommonsWare、ヒントありがとうございます。どのように意図が放送されるのを待つのですか?どのくらいの頻度で放送されていますか?ネットワークIOを実行することを推奨する特定の場所はありますか?これは、私のアプリが行う唯一のもので、ランダムなバイトをダウンロードし、どのくらいのバッテリーレベルが使用されたかを確認することです。 – Michael

+0

@Michael:「放送予定をどうやって待つの?」 - 'onReceive()'からの戻り値。最終的に 'onReceive()'が再び呼び出されます。 https://github.com/commonsguy/cw-advandroid/tree/master/SystemEvents/OnBattery「ネットワークI/Oを実行することを推奨する特定の場所はありますか?」 - AsyncTaskなどのバックグラウンドスレッド。 「これは私のアプリケーションが行う唯一のもので、ランダムなバイトをダウンロードし、使用されたバッテリーの量を確認する」 - ええ、OK。 – CommonsWare

+0

@CommonsWare私はいくつかの変更を加えました...これはどのように見えるのですかhttps://homepage.usask.ca/~mcb394/Receive.java(申し訳ありませんが、フォーマットはいくつかの場所で正しく出てこなかった)。私のAsyncTask内部クラスメソッドからBroadcastReceiverを含むメソッドを確実に呼び出すことはできますか?もっと重要なのは、私はこれを正しく今行っているのですか? – Michael

0

あなたはonReceive()メソッドでは、アプリケーション・ロジックを置くことになっていません。このメソッドは、インテントのみを処理する必要があります。それ以外の場合は、他のアプリへのインテント配信をブロックする可能性があります。

この方法でブロックしている間にインテントが配信されないため、バッテリーの状態は変わりません。渡されたインテント自体は変更されません。 onReceive()メソッドは、バッテリーの状態が変更されたときはいつでも呼び出されます - あなたがそれをブロックしない限り。

+0

申し訳ありませんが、私はアンドロイドに新しいです。私がBroadcastReceiverへのこれらの呼び出しをどのようにするのか、私に知らせることができますか?私はBroadcastReceiverの中で別のメソッドを作成し、そこでコードを記述しますか?次に、そのメソッドの前後にBroadcastReceiverを呼び出してレベルを取得しますか? – Michael

関連する問題