Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用前台服务会在通知栏显示的问题 #1

Open
snowf07 opened this issue Sep 10, 2019 · 1 comment
Open

使用前台服务会在通知栏显示的问题 #1

snowf07 opened this issue Sep 10, 2019 · 1 comment

Comments

@snowf07
Copy link
Owner

snowf07 commented Sep 10, 2019

No description provided.

@snowf07 snowf07 changed the title 前台服务应用保活问题 使用前台服务会在通知栏显示的问题 Sep 10, 2019
@snowf07
Copy link
Owner Author

snowf07 commented Sep 10, 2019

使用前台服务相当于会在用户手机上显示一条常驻通知,所以要特别处理,在Android26以下利用谷歌漏洞处理

/**

  • Created by lang.chen on 2019/9/5
    */
    public class LForegroundService extends Service {

    @OverRide
    public IBinder onBind(Intent intent) {
    return null;
    }
    @OverRide
    public void onCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    startMyOwnForeground();
    }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
    //API Version 18以上
    Notification.Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    startForeground(2, builder.build());
    startService(new Intent(this, InnerService.class));
    }
    }

    public static class InnerService extends Service{
    @OverRide
    public IBinder onBind(Intent intent) {
    return null;
    }
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @OverRide
    public void onCreate() {
    super.onCreate();
    //发送与上面服务中ID相同的Notification,然后将其取消并取消自己的前台显示
    Notification.Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    startForeground(2, builder.build());
    new Handler().postDelayed(new Runnable() {
    @OverRide
    public void run() {
    stopForeground(true);
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.cancel(2);
    stopSelf();
    }
    },100);

    }
    

    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant