Java – Handles commands in Android without starting MainActivity

Handles commands in Android without starting MainActivity… here is a solution to the problem.

Handles commands in Android without starting MainActivity

I’m new to android and at one point got stuck with my new screen off test app. I’m trying to develop an Android app that turns off or locks the screen directly by clicking the app’s app launcher icon.

I was

able to lock the screen with the feature I wanted, but there was a problem. When I click the screen lock icon in the launcher, it takes about a second and then locks the screen (MainActivity takes time to start).

I want that time delay removed and only want to handle the command to lock the phone when the user taps the app icon in the launcher. But I can’t figure it out.

This is the 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 the delay when the screen is locked. Any help would be appreciated. Thanks

Solution

I think you should use the application class

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

}

Related Problems and Solutions