Created
July 11, 2021 21:14
-
-
Save codinginflow/1ab6d2689780be6d764215c3a40f2166 to your computer and use it in GitHub Desktop.
Foreground Service Tutorial
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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" | |
android:orientation="vertical" | |
android:padding="16dp" | |
tools:context="com.codinginflow.foregroundserviceexample.MainActivity"> | |
<EditText | |
android:id="@+id/edit_text_input" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Input" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:onClick="startService" | |
android:text="Start Service" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:onClick="stopService" | |
android:text="Stop Service" /> | |
</LinearLayout> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.codinginflow.foregroundserviceexample"> | |
<application | |
android:name=".App" | |
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"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<service android:name=".ExampleService" /> | |
</application> | |
</manifest> |
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
package com.codinginflow.foregroundserviceexample; | |
import android.app.Application; | |
import android.app.NotificationChannel; | |
import android.app.NotificationManager; | |
import android.os.Build; | |
public class App extends Application { | |
public static final String CHANNEL_ID = "exampleServiceChannel"; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
createNotificationChannel(); | |
} | |
private void createNotificationChannel() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
NotificationChannel serviceChannel = new NotificationChannel( | |
CHANNEL_ID, | |
"Example Service Channel", | |
NotificationManager.IMPORTANCE_DEFAULT | |
); | |
NotificationManager manager = getSystemService(NotificationManager.class); | |
manager.createNotificationChannel(serviceChannel); | |
} | |
} | |
} |
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
package com.codinginflow.foregroundserviceexample; | |
import android.app.Notification; | |
import android.app.PendingIntent; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.os.IBinder; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.NotificationCompat; | |
import static com.codinginflow.foregroundserviceexample.App.CHANNEL_ID; | |
public class ExampleService extends Service { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
String input = intent.getStringExtra("inputExtra"); | |
Intent notificationIntent = new Intent(this, MainActivity.class); | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, | |
0, notificationIntent, 0); | |
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) | |
.setContentTitle("Example Service") | |
.setContentText(input) | |
.setSmallIcon(R.drawable.ic_android) | |
.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; | |
} | |
} |
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
package com.codinginflow.foregroundserviceexample; | |
import android.content.Intent; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.EditText; | |
public class MainActivity extends AppCompatActivity { | |
private EditText editTextInput; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
editTextInput = findViewById(R.id.edit_text_input); | |
} | |
public void startService(View v) { | |
String input = editTextInput.getText().toString(); | |
Intent serviceIntent = new Intent(this, ExampleService.class); | |
serviceIntent.putExtra("inputExtra", input); | |
ContextCompat.startForegroundService(this, serviceIntent); | |
} | |
public void stopService(View v) { | |
Intent serviceIntent = new Intent(this, ExampleService.class); | |
stopService(serviceIntent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment