diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.gitignore b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.gitignore new file mode 100644 index 0000000..2b75303 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.gitignore @@ -0,0 +1,13 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/codeStyles/Project.xml b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..30aa626 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/codeStyles/Project.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/gradle.xml b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/gradle.xml new file mode 100644 index 0000000..5cd135a --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/gradle.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/jarRepositories.xml b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/jarRepositories.xml new file mode 100644 index 0000000..f32880c --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/jarRepositories.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/runConfigurations.xml b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/runConfigurations.xml new file mode 100644 index 0000000..e497da9 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/runConfigurations.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/ForegroundDemo.jpg b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/ForegroundDemo.jpg new file mode 100644 index 0000000..e0ef8ea Binary files /dev/null and b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/ForegroundDemo.jpg differ diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/README.md b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/README.md new file mode 100644 index 0000000..26d15bc --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/README.md @@ -0,0 +1,9 @@ +# ForegroundDemo + +This Android App demonstrates how to make a Foreground Service. + +Visit: [https://github.com/CC-MNNIT/2021-22-Classes/tree/main/Android/2021_05_13_AndroidClass-3/ForegroundDemo/](https://github.com/CC-MNNIT/2021-22-Classes/tree/main/Android/2021_05_13_AndroidClass-3/ForegroundDemo/) for the source code. + +## Outcome: + +![Screenshot](ForegroundDemo.jpg) diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/.gitignore b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/build.gradle b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/build.gradle new file mode 100644 index 0000000..4c4f8d1 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/build.gradle @@ -0,0 +1,30 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 30 + defaultConfig { + applicationId "com.wave.foregroundservice" + minSdkVersion 21 + targetSdkVersion 30 + versionCode 1 + versionName "1.0" + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + implementation 'com.google.android.material:material:1.3.0' + implementation 'androidx.core:core:1.3.2' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' +} diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/proguard-rules.pro b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# 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 *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/androidTest/java/com/wave/foregroundservice/ExampleInstrumentedTest.java b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/androidTest/java/com/wave/foregroundservice/ExampleInstrumentedTest.java new file mode 100644 index 0000000..6a41ae7 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/androidTest/java/com/wave/foregroundservice/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.wave.foregroundservice; + +import android.content.Context; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.wave.foregroundservice", appContext.getPackageName()); + } +} diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/AndroidManifest.xml b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..df3bb30 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/java/com/wave/foregroundservice/ForegroundService.java b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/java/com/wave/foregroundservice/ForegroundService.java new file mode 100644 index 0000000..2675bc7 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/java/com/wave/foregroundservice/ForegroundService.java @@ -0,0 +1,70 @@ +package com.wave.foregroundservice; + +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Intent; +import android.os.Build; +import android.os.IBinder; +import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; + +public class ForegroundService extends Service { + public static final String CHANNEL_ID = "ForegroundServiceChannel"; + + @Override + public void onCreate() { + super.onCreate(); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + String input = intent.getStringExtra("inputExtra"); + createNotificationChannel(); + Intent notificationIntent = new Intent(this, MainActivity.class); + PendingIntent pendingIntent = PendingIntent.getActivity(this, + 0, notificationIntent, 0); + + Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) + .setContentTitle("Foreground Service") + .setContentText(input) + .setSmallIcon(R.drawable.ic_stat_name) + .setContentIntent(pendingIntent) + .build(); + + startForeground(1, notification); + + //do heavy work on a background thread + + + //stopSelf(); + + return START_NOT_STICKY; + } + + @Override + public void onDestroy() { + super.onDestroy(); + } + + @Nullable + @Override + public IBinder onBind(Intent intent) { + return null; + } + + private void createNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel serviceChannel = new NotificationChannel( + CHANNEL_ID, + "Foreground Service Channel", + NotificationManager.IMPORTANCE_DEFAULT + ); + + NotificationManager manager = getSystemService(NotificationManager.class); + manager.createNotificationChannel(serviceChannel); + } + } +} diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/java/com/wave/foregroundservice/MainActivity.java b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/java/com/wave/foregroundservice/MainActivity.java new file mode 100644 index 0000000..0d96a59 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/java/com/wave/foregroundservice/MainActivity.java @@ -0,0 +1,48 @@ +package com.wave.foregroundservice; + +import android.content.Intent; +import android.os.Bundle; +import androidx.core.content.ContextCompat; +import androidx.appcompat.app.AppCompatActivity; + +import android.view.View; +import android.widget.Button; + +public class MainActivity extends AppCompatActivity { + Button btnStartService, btnStopService; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + btnStartService = findViewById(R.id.buttonStartService); + btnStopService = findViewById(R.id.buttonStopService); + + btnStartService.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + startService(); + } + }); + + btnStopService.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stopService(); + } + }); + } + + public void startService() { + Intent serviceIntent = new Intent(this, ForegroundService.class); + serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android"); + + ContextCompat.startForegroundService(this, serviceIntent); + } + + public void stopService() { + Intent serviceIntent = new Intent(this, ForegroundService.class); + stopService(serviceIntent); + } +} + diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-anydpi-v24/ic_stat_name.xml b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-anydpi-v24/ic_stat_name.xml new file mode 100644 index 0000000..a4b3d9b --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-anydpi-v24/ic_stat_name.xml @@ -0,0 +1,14 @@ + + + + + diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-hdpi/ic_stat_name.png b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-hdpi/ic_stat_name.png new file mode 100644 index 0000000..aeb7cbf Binary files /dev/null and b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-hdpi/ic_stat_name.png differ diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-mdpi/ic_stat_name.png b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-mdpi/ic_stat_name.png new file mode 100644 index 0000000..6672201 Binary files /dev/null and b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-mdpi/ic_stat_name.png differ diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1f6bb29 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-xhdpi/ic_stat_name.png b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-xhdpi/ic_stat_name.png new file mode 100644 index 0000000..518e288 Binary files /dev/null and b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-xhdpi/ic_stat_name.png differ diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-xxhdpi/ic_stat_name.png b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-xxhdpi/ic_stat_name.png new file mode 100644 index 0000000..efce1ca Binary files /dev/null and b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-xxhdpi/ic_stat_name.png differ diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable/ic_launcher_background.xml b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..0d025f9 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/layout/activity_main.xml b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..f4b2274 --- /dev/null +++ b/Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,40 @@ + + + +