close
The Wayback Machine - https://web.archive.org/web/20160417102256/http://developer.android.com:80/samples/DeviceOwner/src/com.example.android.deviceowner/MainActivity.html
Show navigation Hide navigation
DeviceOwner / src / com.example.android.deviceowner /

MainActivity.java

1
/*
2
 * Copyright (C) 2015 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.example.android.deviceowner;
18
 
19
import android.app.Activity;
20
import android.app.Fragment;
21
import android.app.admin.DevicePolicyManager;
22
import android.content.Context;
23
import android.os.Bundle;
24
import android.util.Log;
25
 
26
public class MainActivity extends Activity {
27
 
28
    private static final String TAG = "MainActivity";
29
 
30
    @Override
31
    protected void onCreate(Bundle savedInstanceState) {
32
        super.onCreate(savedInstanceState);
33
        setContentView(R.layout.activity_main_real);
34
        if (savedInstanceState == null) {
35
            DevicePolicyManager manager =
36
                    (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
37
            if (manager.isDeviceOwnerApp(getApplicationContext().getPackageName())) {
38
                // This app is set up as the device owner. Show the main features.
39
                Log.d(TAG, "The app is the device owner.");
40
                showFragment(DeviceOwnerFragment.newInstance());
41
            } else {
42
                // This app is not set up as the device owner. Show instructions.
43
                Log.d(TAG, "The app is not the device owner.");
44
                showFragment(InstructionFragment.newInstance());
45
            }
46
        }
47
    }
48
 
49
    private void showFragment(Fragment fragment) {
50
        getFragmentManager().beginTransaction()
51
                .replace(R.id.container, fragment)
52
                .commit();
53
    }
54
 
55
}