Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:40:12+00:00 2026-06-15T11:40:12+00:00

I am trying to lock a device programmatically when the user presses a button.

  • 0

I am trying to lock a device programmatically when the user presses a button. I am aware that I will need to use deviceAdminReciever and I have done so but my app crashes whenever I run it

The following is my manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyApp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".MainActivity"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/device_admin_sample" />

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>
</application>

<uses-feature android:name="android.hardware.camera" />

</manifest>

The following is my java code:

public class MainActivity extends DeviceAdminReceiver {

public static class MyActivity extends Activity {

    protected static final int ACTIVATION_REQUEST = 1;
    private ImageButton btn;

    private DevicePolicyManager mDPM;
    private ComponentName mDeviceAdminSample;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        mDeviceAdminSample = new ComponentName(Potter.this,
                MainActivity.class);
        setContentView(R.layout.activity_main);



        btn = (ImageButton) findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(
                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            mDeviceAdminSample = new ComponentName(this, MainActivity.class);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    mDeviceAdminSample);
            startActivityForResult(intent, ACTIVATION_REQUEST);
            mDPM.lockNow();

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
         case ACTIVATION_REQUEST:
                if (resultCode == Activity.RESULT_OK) {
                    Log.i("DeviceAdminSample", "Administration enabled!");
                } else {
                    Log.i("DeviceAdminSample", "Administration enable FAILED!");
                }
                return;


        }
    }


}
}

However, when I run the app it crashes. How can I correct this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:40:13+00:00Added an answer on June 15, 2026 at 11:40 am

    OK, DeviceAdminReceiver is a BroadcastReceiver, not an Activity. Right now, your manifest declares MainActivity for both components, so one of those declarations is incorrect. MainActivity is a bad name for this class since it is not an Activity, it should probably be MainReceiver or something like that (just for consistency’s sake).

    Your application is crashing because Android is trying to start MainActivity, which is not an Activity, as the main Activity of your application, which it cannot do.

    Also, according to your code, MyActivity is an inner class of this receiver. This is not a paradigm I would recommend sticking with and may be leading to some of your confusion. I would define both of these entities as completely separate classes. If one MUST be an inner class of the other, the BroadcastReceiver will make more sense as an inner class of the Activity.

    At the BARE MINIMUM, if you don’t refactor any of your Java code, you need to update your manifest to reference the proper elements based on what you’ve written, which means referencing the actual Activity as an inner class.

    <activity
        android:name=".MainActivity$MyActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
    <receiver
        android:name=".MainActivity"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/device_admin_sample" />
    
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>
    

    Perhaps take a moment to review again the Device Administration API Sample in the SDK, which is located at

    <SDK location>/samples/<platform-version>/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java
    

    on your machine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're trying to lock down a computer such that we have a generic login
I'm trying to use the systemLock() to lock the device when the getSpeed() returns
I am trying to lock the display (disallowing the user to use the keyboard
I was trying to lock the device and trying to enable it by using
I am trying to work on a simple android app that when the device
i'm trying to lock a row in a db-table when a user is editing
I'm trying to create an app that when installed on a device allow me
I have a Rails 3 app that I am trying to implement devise and
I have a Rails 3 app that I am trying to implement devise and
I'm trying to guess why, if I open my application, lock the device and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.