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

希望能像李跳跳一样,跳过广告时显示通知,这个功能可以自定义显示的内容。以及常驻通知,减少被系统杀的概率,希望也能自定义常驻通知显示的内容 #86

Open
3025808734 opened this issue Nov 18, 2023 · 3 comments

Comments

@3025808734
Copy link

No description provided.

@1145046505
Copy link

自建创建一个AliveService类,常驻通知栏留一个前台服务确实不会被啥后台(锁定app后台的情况下
1 AliveService.java

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

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

public class AliveService extends Service {

private static final int NOTIFICATION_ID = 1;
private static final String CHANNEL_ID = "KeepAliveChannel";

@Override
public void onCreate() {
    super.onCreate();
    createNotificationChannel();
}

@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, CHANNEL_ID)
            .setContentTitle("开屏跳过")
            .setContentText("正在检测中,请勿杀掉进程")
            .setSmallIcon(R.drawable.ic_notification)
            .setContentIntent(pendingIntent)
            .build();

    startForeground(NOTIFICATION_ID, notification);
    // Execute the task that needs to run in foreground
    return START_NOT_STICKY;
}

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


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

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

}

2、Mainfests里面加入一个 service标签
<service android:name=".AliveService" android:enabled="true" />

目前我用的VIVO 手机,之前老是被杀掉后台,而且无障碍权限还会时不时没有,自己加了个前台通知栏服务,好用多了
以上代码自己拿AS加进入编译一下即可,现成代码

@1145046505
Copy link

少了一个权限

@1145046505
Copy link

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

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

2 participants