Process command without starting MainActivity, on Android

I'm new to android and stuck at the same place with a new screen testing app. I am trying to develop an android app to disable or lock the screen directly by clicking the app launcher icon.

I can lock the screen with the functionality I want, but with one problem. Although I click on the lock screen icon in the launcher , it takes about a second and then locks the screen (time starts in MainActivity to start).

I want the time delay to be removed and just want to process the command to lock the phone when the user clicks the app icon in the launcher. But I am not able to understand this.

Here is AndroidManifest.xml

<application
    android:theme="@android:style/Theme.NoDisplay"
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:allowBackup="true">

    <activity android:name=".MainActivity"
        android:excludeFromRecents="true"
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="ScreenOffAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data android:name="android.app.device_admin"
            android:resource="@xml/permissions" />
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>

</application>

      

MainActivity.java

public class MainActivity extends Activity {

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        DevicePolicyManager deviceManger = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName compName = new ComponentName(this, ScreenOffAdminReceiver.class);
        if (deviceManger.isAdminActive(compName)) {
            deviceManger.lockNow();
            finish();
        } else {
            Intent intent = new Intent("android.app.action.ADD_DEVICE_ADMIN");
            intent.putExtra("android.app.extra.DEVICE_ADMIN", compName);
            intent.putExtra("android.app.extra.ADD_EXPLANATION", getString(R.string.devicePolicyManagerMsg));
            startActivityForResult(intent, 0);
        }
        Process.killProcess(Process.myPid());
    }
}

      

ScreenOffAdminReceiver.java

public class ScreenOffAdminReceiver extends DeviceAdminReceiver {
    public void onDisabled(Context context, Intent intent) {
        Toast.makeText(context, R.string.deviceAdminDisabled, Toast.LENGTH_SHORT).show();
    }

    public void onEnabled(Context context, Intent intent) {
        Toast.makeText(context, R.string.deviceAdminEnabled, Toast.LENGTH_SHORT).show();
    }
}

      

permissions.xml

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <force-lock />
    </uses-policies>
</device-admin>

      

I just want to remove DELAY while locking the screen. Any help would be much appreciated. Thanks to

+3


source to share


5 answers


I think you should be using Application Class



public class MainApplication  extends Application {
 @Override
    public void onCreate() {
        super.onCreate();
/*
        you should code here */
}

}

      

+4


source


You can try to reduce your app startup time (create activity) with change theme settings:

Try using in your application style:



<item name="android:windowDisablePreview">true</item>
<item name="android:windowContentOverlay">@null</item>

      

+2


source


I think this will help you.

public class MainActivity extends AppCompatActivity {

    DevicePolicyManager deviceManger;
    ComponentName compName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        deviceManger = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
        compName = new ComponentName(this, ScreenOffAdminReceiver.class);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (deviceManger.isAdminActive(compName)) {
            deviceManger.lockNow();
            finish();
        } else {
            Intent intent = new Intent("android.app.action.ADD_DEVICE_ADMIN");
            intent.putExtra("android.app.extra.DEVICE_ADMIN", compName);
            intent.putExtra("android.app.extra.ADD_EXPLANATION", "Device Policies");
            startActivityForResult(intent, 0);
        }
    }
}

      

0


source


I don't think you could achieve near-zero response time while your "trigger" clicks on the application launcher icon for two reasons:

  • When you click the application icon, the request is sent to the "system" (if you want to know more about this, this is called the zygote process). The system then creates a startup process for your application, and only then does the application launch. This is guaranteed to take some time.

  • The first code to be executed is your application method onCreate()

    , but if your application is still stored in the background / phone memory, this method will not be executed when you click on the application launcher icon, so this solution is not really what you are looking for.

0


source


I think it will be faster if, instead of locking the screen from within the app, you can create a widget to do the same. It will be faster and more efficient. This might help you

0


source







All Articles