GithubHelp home page GithubHelp logo

dcn01 / android-2020-app- Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yunianvh/android-2020-app-

0.0 1.0 0.0 3 KB

Android应用的保活、拉活,2020真正杀不死的APP源码,适配Android8.0到Android11所有机型。

android-2020-app-'s Introduction

图片为证


图1 程序自动拉活 图2 打不开的进程页面

保活思路


       传统的套路咱就不再累赘(详情可看这里),这里分享一个流氓做法,具体看下面代码。

	private static android.os.Handler handler = new android.os.Handler(Looper.getMainLooper());
	/**
     * 返回主页
     *
     * @param context
     * @param delay
     */
    public static void perforGlobalHome(final Context context, long delay) {
        Intent home = new Intent(Intent.ACTION_MAIN);
        home.addCategory(Intent.CATEGORY_HOME);
        home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(home);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent home = new Intent(Intent.ACTION_MAIN);
                home.addCategory(Intent.CATEGORY_HOME);
                home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(home);
            }
        }, delay);
    }

       很多大佬看到这里可能就不耐烦了,这不就是通过代码拉回主页嘛,跟保活有毛关系。

       不着急,再配上一个广播监听基本就实现杀不死了,具体看代码。

public class BroadcastReceiver extends android.content.BroadcastReceiver {
    private final String TAG = BroadcastReceiver.class.getSimpleName();

    @Override
    public void onReceive(final Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.e(TAG, "接收到广播:" + intent.getAction());
        
        if (intent.getAction() == Intent.ACTION_CLOSE_SYSTEM_DIALOGS) {
            perforGlobalHome(context, 500);
        }
     }
}

       到这里就很多大佬应该能看出来了,实际上就是监听ACTION_CLOSE_SYSTEM_DIALOGS,当触发时直接通过代码拉回桌面,这样用户打不开进程页面自然就杀不死程序进程了。

       哦,别忘了注册广播,详情看下面代码。

 IntentFilter filter = new IntentFilter();
 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);//home
 registerReceiver(new BroadcastReceiver(), filter);

       简单而又粗暴,无需任何权限即可实现应用保活,妈妈再也不用担心我Android O(8)到Android Q(10)无法实现应用保活的问题了。

拉活权限


       当然了,高标准需求的小伙伴都应该知道,接下来才是正菜,仅仅防止用户手动杀掉进程还是远远不够的,我们并不能排除应用被系统干掉的可能性。

       那么我们又是如何来解决这个问题呢?详情请看下面具体代码。

public class MyAccessibilityService extends AccessibilityService {
    private String TAG = MyAccessibilityService.class.getSimpleName();
    protected void onServiceConnected() {
        super.onServiceConnected();       
    }

    @Override
    protected boolean onKeyEvent(KeyEvent event) {
        return false;
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
    	//在这里重新拉活自己的程序即可
    }
    
	@Override
    public void onInterrupt() {
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
}

       不要忘了在AndroidManifest中注册服务完成配置具体看下面代码。

		<service
            android:name=".servise.MyAccessibilityService"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>
            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/accessibility_service_config" />
        </service>

       当然了,还有xml下的配置文件,具体看代码。

	<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeWindowStateChanged"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagIncludeNotImportantViews"/>

完整代码


gitee:https://gitee.com/yunianvh/employee-management-android github:https://github.com/Life1412378121/Android-2020-APP- CSDN:https://blog.csdn.net/qq_35350654/article/details/109235852

android-2020-app-'s People

Contributors

yunianvh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.