-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff64431
commit 97c83cf
Showing
46 changed files
with
975 additions
and
1 deletion.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
Android/2021_05_13_AndroidClass-3/ForegroundDemo/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
29 changes: 29 additions & 0 deletions
29
Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/codeStyles/Project.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/gradle.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/jarRepositories.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
Android/2021_05_13_AndroidClass-3/ForegroundDemo/.idea/runConfigurations.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
1 change: 1 addition & 0 deletions
1
Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
30 changes: 30 additions & 0 deletions
30
Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
} |
21 changes: 21 additions & 0 deletions
21
Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/proguard-rules.pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
26 changes: 26 additions & 0 deletions
26
...oundDemo/app/src/androidTest/java/com/wave/foregroundservice/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
@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()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Android/2021_05_13_AndroidClass-3/ForegroundDemo/app/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.wave.foregroundservice"> | ||
|
||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<service | ||
android:name=".ForegroundService" | ||
android:enabled="true" | ||
android:exported="true"></service> | ||
|
||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
70 changes: 70 additions & 0 deletions
70
...lass-3/ForegroundDemo/app/src/main/java/com/wave/foregroundservice/ForegroundService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...roidClass-3/ForegroundDemo/app/src/main/java/com/wave/foregroundservice/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
...05_13_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-anydpi-v24/ic_stat_name.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:tint="#FFFFFF" | ||
android:viewportWidth="26.086956" | ||
android:viewportHeight="26.086956"> | ||
<group | ||
android:translateX="1.0434783" | ||
android:translateY="1.0434783"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M12,23c1.1,0 1.99,-0.89 1.99,-1.99h-3.98c0,1.1 0.89,1.99 1.99,1.99zM20.29,18.29L19,17v-6c0,-3.35 -2.36,-6.15 -5.5,-6.83L13.5,3c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v1.17C7.36,4.85 5,7.65 5,11v6l-1.29,1.29c-0.63,0.63 -0.19,1.71 0.7,1.71h15.17c0.9,0 1.34,-1.08 0.71,-1.71zM13,16h-2v-2h2v2zM13,11c0,0.55 -0.45,1 -1,1s-1,-0.45 -1,-1L11,9c0,-0.55 0.45,-1 1,-1s1,0.45 1,1v2z" /> | ||
</group> | ||
</vector> |
Binary file added
BIN
+355 Bytes
...3_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-hdpi/ic_stat_name.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+250 Bytes
...3_AndroidClass-3/ForegroundDemo/app/src/main/res/drawable-mdpi/ic_stat_name.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.