diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..a2ebf14 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +BatteryStatus \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..9a8b7e5 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..1bbc21d --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6a1e020 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8cc58c5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..22210a8 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 22 + buildToolsVersion "22.0.1" + + defaultConfig { + applicationId "com.example.batterystatus" + minSdkVersion 17 + targetSdkVersion 22 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:22.2.1' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..59a59ac --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/nanao/Library/Android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/androidTest/java/com/example/batterystatus/ApplicationTest.java b/app/src/androidTest/java/com/example/batterystatus/ApplicationTest.java new file mode 100644 index 0000000..32bb904 --- /dev/null +++ b/app/src/androidTest/java/com/example/batterystatus/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.example.batterystatus; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..d95e168 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/example/batterystatus/BatteryWatchService.java b/app/src/main/java/com/example/batterystatus/BatteryWatchService.java new file mode 100644 index 0000000..8c62466 --- /dev/null +++ b/app/src/main/java/com/example/batterystatus/BatteryWatchService.java @@ -0,0 +1,57 @@ +package com.example.batterystatus; + +import android.app.Service; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.BatteryManager; +import android.os.IBinder; +import android.util.Log; + +import java.util.Timer; +import java.util.TimerTask; + +public class BatteryWatchService extends Service { + static String TAG = "BatteryWatchService"; + private Timer timer; + + public BatteryWatchService() { + } + + @Override + public IBinder onBind(Intent intent) { + Log.d(TAG, "onBind"); + return null; + } + + @Override public void onCreate() { + super.onCreate(); + Log.d(TAG, "onCreate"); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + Log.d(TAG, "onStartCommand"); + timer = new Timer(); + timer.schedule(new TimerTask() { + @Override + public void run() { + IntentFilter intentfilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + Intent batteryStatus = registerReceiver(null, intentfilter); + + if (batteryStatus!=null) { + int batteryLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); + Log.d(TAG, "Level: " + String.valueOf(batteryLevel)); + } + } + }, 0, 5000); + + return START_STICKY; + } + + @Override public void onDestroy() { + Log.d(TAG, "onDestroy"); + timer.cancel(); + + super.onDestroy(); + } +} diff --git a/app/src/main/java/com/example/batterystatus/MainActivity.java b/app/src/main/java/com/example/batterystatus/MainActivity.java new file mode 100644 index 0000000..189747c --- /dev/null +++ b/app/src/main/java/com/example/batterystatus/MainActivity.java @@ -0,0 +1,56 @@ +package com.example.batterystatus; + +import android.content.Intent; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.Button; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + Button startButton = (Button) findViewById(R.id.start_button); + Button stopButton = (Button) findViewById(R.id.stop_button); + + startButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + startService(new Intent(getApplicationContext(), BatteryWatchService.class)); + } + }); + stopButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stopService(new Intent(getApplicationContext(), BatteryWatchService.class)); + } + }); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } + + return super.onOptionsItemSelected(item); + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..b0f553a --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,20 @@ + + +