0

I want to make a foreground service that calls an Asynchrounic task every few seconds. so I defined a foreground service like this:(I haven't added the task yet)



import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import static com.example.notif.App.ID;

public class Service extends android.app.Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Intent notificationIntent= new Intent(this, MainActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

        Notification notification = new NotificationCompat.Builder(this,ID)
                .setContentTitle("Title")
                .setContentText("Text")
                .setSmallIcon(R.drawable.ic_android)
                .setContentIntent(pendingIntent)
                .build();


        startForeground(2,notification);


        return START_STICKY;
    }



}

edited the manifest like this and added the service to it.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notif">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:usesCleartextTraffic="true"

        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Notif">
        <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=".Service"></service> //i added service here
    </application>

</manifest>

and I start it with an onClickListener:

public void start(View view) {
        Intent serviceIntent = new Intent(this,Service.class);
        startService(serviceIntent);
    }

when I press the button the app crashes. what am I doing wrong? how should I proceed?

Anthraxff
  • 148
  • 1
  • 13

2 Answers2

0

If your app targets API level 26 or higher and you want to create foreground service you should call startForegroundService() instead of startService()

i30mb1
  • 3,894
  • 3
  • 18
  • 34
0

You should use ContextCompat.startForegroundService() because it automatically handles post and pre Oreo behavior like below

public static void startForegroundService(@NonNull Context context, @NonNull Intent intent) {
    if (Build.VERSION.SDK_INT >= 26) {
        context.startForegroundService(intent);
    } else {
        // Pre-O behavior.
        context.startService(intent);
    }
}

Also add the permission

 <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

Apps that target Android 9 (API level 28) or higher and use foreground services must request the FOREGROUND_SERVICE permission

Note: If an app that targets API level 28 or higher attempts to create a foreground service without requesting the FOREGROUND_SERVICE permission, the system throws a SecurityException.

akhil nair
  • 1,371
  • 1
  • 11
  • 19