2020-09-29

Android Foreground Service with Notification wont start

My foreground service wont start. The printout in onCreate() does not even get called. I am trying to run on Android 9. My min, target, compile API versions are 26,26, 28.

I am expecting the service should call onCreate, start and show a notification on top.

What seems to be the problem? Is it because of the android version? I have androidx enabled FYI.

Manifest file:

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACTION_OPEN_DOCUMENT" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <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"
        android:largeHeap="true"
        android:hardwareAccelerated="true"
        android:debuggable="true"
        >


        <activity
                android:name="com.example.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:resizeableActivity="false"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name="com.example.MDFSService"
            android:enabled="true"
            android:stopWithTask="true">
        </service>
    </application>

Service.java


//imports

//this is the service class
public class ServiceClass extends Service {

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

    @Override
    public void onCreate(){
        System.out.println("starting service");
    }

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

        showNotificationAndStartService("Started");
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {

        System.out.println("Stopping service");
        super.onDestroy();

    }



    public static final String CHANNEL_ID = RDRIVEService.class.getName();
    public void showNotificationAndStartService(String message) {
        String input = intent.getStringExtra("inputExtra");
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        long[] pattern = {0, 500, 0 };
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle(message)
                .setContentText(message)
                .setContentText(input)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentIntent(pendingIntent)
                .setVibrate(pattern)
                .build();

        try {
            startForeground(1, notification);
        } catch (RuntimeException e){
            Toast.makeText(this, "Foreground permission not granted", Toast.LENGTH_LONG).show();
            this.onDestroy();
        }

    }


    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);
        }
    }

}


from Recent Questions - Stack Overflow https://ift.tt/36bMq6t
https://ift.tt/eA8V8J

No comments:

Post a Comment