(續2)Device Administration 裝置管理

Source URL
https://developer.android.com/guide/topics/admin/device-admin.html

前一篇文章在此
https://javahand.blogspot.tw/2017/12/device-administration.html


public class MainActivity
 extends FragmentActivity
 implements StatusFragment.StatusUpdatedListener
{
 protected void onCreate( Bundle savedInstanceState )
 {
  ...

  if ( null == savedInstanceState )
  {
   DevicePolicyManager devicePolicyManager = (DevicePolicyManager)      getSystemService( Context.DEVICE_POLICY_SERVICE );

   PackageManager packageManager = getPackageManager();



   if ( !devicePolicyManager.isProfileOwnerApp(

     getApplicationContext().getPackageName()))
   { // 如果管理項目尚未設置,將顯示設定畫面。
    showSetupProfile();

   }
   else

   {
    try
    { 
     ----------------------------------------------
      移至下方再進行了解 — 程式碼 A
     ----------------------------------------------
    }
    catch ( PackageManager.NameNotFoundException e )
    {
     showStatusProfile();
    } // end Try-Catch
   } // end If

  } // end If
 } // end Method onCreate( Bundle )

 private void showSetupProfile()
 {
  getSupportFragmentManager().beginTransaction().replace(

    R.id.container, SetupProfileFragment.newInstance()).commit();
    -------------------------------------------------------------
    R.id.container
    >> activity_main_real.xml
     <LinearLayout>
      <FrameLayout>

       ...
      <FrameLayout

        id="container"
      />
    -------------------------------------------------------------
    SetupProfileFragment.newInstance()
    >> SetupProfileFragment.java
     public class SetupProfileFragment
       extends Fragment
       implements View.OnClickListener
     {
      ...
 
      public static SetupProfileFragment newInstance()
      {
       return new SetupProfileFragment();

      } // end Method newInstance()
 

      public SetupProfileFragment()
      {
      } // end Constructor SetupProfileFragment()
 

      public View onCreateView( LayoutInflater inflater, 
        ViewGroup container, Bundle savedInstanceState )
      {
       return inflater.inflate(

         R.layout.fragment_setup_profile, container, false );
         ----------------------------------------------------
         R.layout.fragment_setup_profile

         >> fragment_setup_profile.xml
          <ScrollView>
           <LinearLayout>
            <TextView
             text="@string/setup_profile_message"

             ---------------------------------------------
             >> strings.xml
              <string name="setup_profile_message">This app is currently not a profile owner.</string>
             ---------------------------------------------

            />
            <Button
             text="@string/set_up_profile"
             ---------------------------------------------
             >> strings.xml
              <string name="set_up_profile">Set up profile</string>
             ---------------------------------------------
            />
         ----------------------------------------------------      } // end Method onCreateView( LayoutInflater, ... )
     } // end Class SetupProfileFragment
    -------------------------------------------------------------
 } // end Method showSetupProfile()
} // end Class MainActivity




----------------------------------------------
 程式碼 A
----------------------------------------------
int packageFlags;

if ( Build.VERSION.SDK_INT < 24)

{ //noinspection deprecation
 packageFlags = PackageManager.GET_UNINSTALLED_PACKAGES;
}

else
{
 packageFlags = PackageManager.MATCH_UNINSTALLED_PACKAGES;
} // end If-Else


ApplicationInfo info = packageManager.getApplicationInfo( 

  Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA, packageFlags );
  --------------------------------------------------------------
  Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA
  >> Constants.java
   public interface Constants
   {
    String PACKAGE_NAME_APP_RESTRICTION_SCHEMA = "com.example.android.apprestrictionschema";
   } // end Interface Constants
  --------------------------------------------------------------

if ( 0 == ( info.flags & ApplicationInfo.FLAG_INSTALLED))
{ // 範例程式必須重新安裝
 showStatusProfile();
}

else if ( devicePolicyManager.isApplicationHidden(
  EnforcerDeviceAdminReceiver.getComponentName( this ),
  Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA ))
{ // The app is installed but hidden in this profile
 showStatusProfile();
}

else
{ // Everything is clear; show the main screen
 showMainFragment();
} // end If-ElseIf-Else







未完待續...


留言

這個網誌中的熱門文章

泰文子音與字形之間的變化

AUTOMAXX UP-5HX 使用心得