Refactor platform_services sample (#8949)
* Refactor platform_services sample * removed exception
This commit is contained in:
@@ -8,12 +8,8 @@ import android.support.test.runner.AndroidJUnit4;
|
||||
import io.flutter.view.FlutterView;
|
||||
|
||||
import android.app.Instrumentation;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -23,8 +19,8 @@ import static org.junit.Assert.*;
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ExampleInstrumentedTest {
|
||||
@Rule
|
||||
public ActivityTestRule<ExampleActivity> activityRule =
|
||||
new ActivityTestRule<>(ExampleActivity.class);
|
||||
public ActivityTestRule<MainActivity> activityRule =
|
||||
new ActivityTestRule<>(MainActivity.class);
|
||||
|
||||
|
||||
@Test
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
<application android:name="io.flutter.app.FlutterApplication" android:label="@string/app_name" >
|
||||
<activity
|
||||
android:name=".ExampleActivity"
|
||||
android:name=".MainActivity"
|
||||
android:launchMode="singleTop"
|
||||
android:theme="@android:style/Theme.Black.NoTitleBar"
|
||||
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
|
||||
|
||||
@@ -18,7 +18,7 @@ import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
|
||||
import io.flutter.plugin.common.FlutterMethodChannel.Response;
|
||||
import io.flutter.plugin.common.MethodCall;
|
||||
|
||||
public class ExampleActivity extends FlutterActivity {
|
||||
public class MainActivity extends FlutterActivity {
|
||||
private static final String CHANNEL = "battery";
|
||||
|
||||
@Override
|
||||
@@ -30,15 +30,19 @@ public class ExampleActivity extends FlutterActivity {
|
||||
@Override
|
||||
public void onMethodCall(MethodCall call, Response response) {
|
||||
if (call.method.equals("getBatteryLevel")) {
|
||||
getBatteryLevel(response);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unknown method " + call.method);
|
||||
int batteryLevel = getBatteryLevel();
|
||||
|
||||
if (batteryLevel != -1) {
|
||||
response.success(batteryLevel);
|
||||
} else {
|
||||
response.error("UNAVAILABLE", "Battery level not available.", null);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void getBatteryLevel(Response response) {
|
||||
private int getBatteryLevel() {
|
||||
int batteryLevel = -1;
|
||||
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
|
||||
BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);
|
||||
@@ -50,10 +54,6 @@ public class ExampleActivity extends FlutterActivity {
|
||||
intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
|
||||
}
|
||||
|
||||
if (batteryLevel != -1) {
|
||||
response.success(batteryLevel);
|
||||
} else {
|
||||
response.error("UNAVAILABLE", "Battery level not available.", null);
|
||||
}
|
||||
return batteryLevel;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user