Realize the method in rumor QQ - "1 pixel page keep alive"

The first step is to create a new Activity, as the main body of the 1-pixel page, I will call it HooliganActivity:

public class HooliganActivity extends Activity {
    private static final String TAG = "HooliganActivity";
    private static HooliganActivity instance;

    /**
     * Open the keep alive page
     */
    public static void startHooligan() {
        Log.d(TAG, "startHooligan: start the keep alive page");
        Intent intent = new Intent(My_Application.getContext(), HooliganActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        My_Application.getContext().startActivity(intent);
    }

    /**
     * Close the keep alive page
     */
    public static void killHooligan() {
        Log.d(TAG, "killHooligan: close the keepalive page");
        if (instance != null) {
            instance.finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        instance = this;
        Window window = getWindow();
        window.setGravity(Gravity.LEFT | Gravity.TOP);
        WindowManager.LayoutParams params = window.getAttributes();
        params.x = 0;
        params.y = 0;
        params.height = 1;
        params.width = 1;
        window.setAttributes(params);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy ();
        instance = null;
    }
}

The second step is to register the manifest file:

<activity
            android:name=".view.HooliganActivity"
            android:configChanges="keyboardHidden|orientation|screenSize|navigation|keyboard"
            android:excludeFromRecents="true"
            android:exported="false"
            android:finishOnTaskLaunch="false"
            android:launchMode="singleInstance"
            android:theme="@style/HooliganActivityStyle" />
<style name="HooliganActivityStyle">
        <item name="android:windowBackground">@color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoDisplay">false</item>
        <item name="android:windowDisablePreview">true</item>
    </style>
<!--Android full transparent color-->
    <color name="transparent">#00000000</color>

The third step is to monitor the lock screen and unlock notifications. You cannot statically register broadcasts, but only dynamically register: [Can be placed in MainActivity, BaseActivity, or SplashActivity]

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BootCompleteReceiver(), filter);

The fourth step, wake up my HooliganActivity when unlocking and locking the screen respectively:

/**
 * Created by 柱子 on 2018/04/13
 */
public class NetBroadcastReceiver extends BroadcastReceiver {
    public NetEvevt netEvevt = BaseActivity.evevt;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("eeee", "onReceive: ");
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            HooliganActivity.startHooligan();
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            HooliganActivity.killHooligan();
        }
    }
}
In this way, every time you lock the screen in the background, you will actually hang up a one-pixel page, pretending that the app is in the foreground and has the highest process priority.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325556830&siteId=291194637