Search code examples
javaandroidcrashforeground-service

I'm trying to run foreground service in android but whenever i run my app, app got freezed and not showing any notification


I have created a Notification channel In the onCreate method of MapsActivity and called the method startService to start the service.

App stay froze until service execution ends

In logcat Errors caused by is not mentioned. I have also mentioned using FOREGROUND_SERVICE in ManifestFile

Main Class

public class MapsActivity extends AppCompatActivity {
    public static final String CHANNEL_ID = "FenceCalculateChannel";
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
createNotificationChannel();
        Intent serviceIntent = new Intent(this,BackGroundDistanceCalculate.class);
        startService(serviceIntent);
}
public void createNotificationChannel() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID,
                    "Fence Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT);

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }
}

BackGroundDistanceCalculate Service Class

public class BackGroundDistanceCalculate extends Service {

    final public static String TAG = "BackGroundDistance";
    @Override
    public void onCreate() {
        super.onCreate();
    }

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

        Intent notificationIntent = new Intent(this, MapsActivity.class);
        PendingIntent pendingIntent =
                PendingIntent.getActivity(this, 0, notificationIntent, 0);

        int i;

        for(i=0;i<1000;i++)
        {
            Log.d(TAG,"Value of i is : "+ i);
            SystemClock.sleep(1000);
        }

        Notification notification =
                new NotificationCompat.Builder(this, CHANNEL_ID)
                        .setContentTitle("Calculating Distance")
                        .setContentText("Distance Calculation is running")
                        .setSmallIcon(R.drawable.ic_favorite)
                        .setContentIntent(pendingIntent)
                        .build();

        //startForeground(1, notification);
        return START_NOT_STICKY;
    }

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


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

This code should create a Notificationchannel on App start but not working. What changes may I make to make this App run?


Solution

  • Service runs works on main thread (UI thread) according to official doc, so it blocked UI, that's why your app freezed.

    You should use IntentService which already has a worker thread to run your work.

    Or you can manually create an AsyncTask or a Thread inside your service to handle your work.