Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: how to start a service from oreo on

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default how to start a service from oreo on

    hi from oreo you start a service differently than before and then it changed again for s and i cant figure out what is wrong with my pendingIntent
    Intent notificationIntent = new Intent(this, blockservice.class);
            PendingIntent pendingIntent;
     
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {//THIS WORKS
                pendingIntent = getActivity(this, 0, notificationIntent, 0);
                System.out.println("older than o sdk");
            }else if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.S && Build.VERSION.SDK_INT <= Build.VERSION_CODES.O){//DOES NOT WORK
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.createNotificationChannel(new NotificationChannel("micMuteChannel", "micMute", NotificationManager.IMPORTANCE_HIGH));
                System.out.println("sdk between o+s");//this on works on lineage
            }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){//DOES NOT WORK
                System.out.println("sdk s or above");
                PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
            }
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "micMuteChannel");
                Notification notification = notificationBuilder.setOngoing(true)
                        .setContentTitle(this.getString(R.string.app_name))
                        .setPriority(NotificationManager.IMPORTANCE_HIGH)
                        .setCategory(Notification.CATEGORY_SERVICE).build();
            try {
                    startForeground(1337, notification);
                }catch(Exception e){
                    e.printStackTrace();
                }
    The error is

    Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=micMuteChannelappname pri=4 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 category=service vis=PRIVATE)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

    have searche for examples and have tried allot of things but i keep getting this error for all emulators running oreo and above if anyone can spot my mistake would be great thanks

  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: how to start a service from oreo on

    k just an update i have it working for oreo to s incase it is of use to others
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                System.out.println("------------------------------- older than o sdk");
                Intent notificationIntent = new Intent(this, blockservice.class);
                PendingIntent pendingIntent = getActivity(this, 0, notificationIntent, 0);
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "micMuteChannel");
                Notification notification = notificationBuilder.setOngoing(true)
                        .setContentTitle(this.getString(R.string.app_name))
                        .setPriority(NotificationManager.IMPORTANCE_HIGH)
                        .setCategory(Notification.CATEGORY_SERVICE).build();
                try {
                    startForeground(1337, notification);
                }catch(Exception e){
                    e.printStackTrace();
                }

    i now just need to figure out how to launch a service from s using the code from above gives error

     
    Bad notification for startForeground

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: how to start a service from oreo on

    k have it working in case it is of use to others
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {//Build.VERSION.SDK_INT < Build.VERSION_CODES.O
                System.out.println("------------------------------- older than o sdk");
                Intent notificationIntent = new Intent(this, blockservice.class);
                PendingIntent pendingIntent = getActivity(this, 0, notificationIntent, 0);
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "name");
                Notification notification = notificationBuilder.setOngoing(true)
                        .setContentTitle(this.getString(R.string.app_name))
                        .setPriority(NotificationManager.IMPORTANCE_HIGH)
                        .setCategory(Notification.CATEGORY_SERVICE).build();
                try {
                    startForeground(1337, notification);
                } catch (Exception e) {
                    e.printStackTrace();
                }
     
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//Build.VERSION.SDK_INT <= Build.VERSION_CODES.S && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
                System.out.println("------------------------------- 0 or highter sdk");
                final NotificationCompat.Builder notification = new NotificationCompat.Builder(this,"name")
                        .setContentTitle("name")
                        .setSmallIcon(R.drawable.a)
                        .setOnlyAlertOnce(true)
                        .setOngoing(true);
                final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                startForeground(1334,notification.build());
     
            }

  4. The Following User Says Thank You to MrBean For This Useful Post:

    Norm (March 1st, 2023)

Similar Threads

  1. Replies: 0
    Last Post: February 19th, 2020, 05:39 AM
  2. How can i start doing web service in java using eclipse
    By Sharonswaroop in forum Android Development
    Replies: 2
    Last Post: December 18th, 2013, 01:06 AM
  3. Regarding service adding
    By sabarimanoj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 28th, 2013, 01:16 AM