安卓开发6-让安卓OA开机自动启动

采用BroadcastReceiver实现安卓应用开机自动启动使用KeyguardManager解除屏幕锁定。前面已经完成一个安卓OA,通过一个WebView访问一个适应手机大小的web oa,并且自动检测有消息时提醒。当用户关机后再开机可能会忘记访问OA,这样会漏掉重要的审批文件。这里采用BroadcastReceiver为安卓OA增加开机自动启动。

用BroadcastReceiver实现开机自启动

         (一)新建BroadcastReceiver类:New – class – Superclass选择为“android.content.BroadcastReceiver”—name我用“BootBroadcastReceive”。完成后BootBroadcastReceive重载onReceive方法

      Intent intent = new Intent(arg0, MainActivity.class);

      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      arg0.startActivity(intent);

         其中arg0是onReceive第一个参数Context.

         (二)、在mainifest.xml定义BootBroadcastReceiver

        <receiver android:name="BootBroadcastReceiver"> 

                            <intent-filter> 

                     <action android:name="android.intent.action.BOOT_COMPLETED"></action> 

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

                            </intent-filter> 

                   </receiver>

         (三)、在manifest.xml配置文件中增加权限:

         <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

让用户选择是否开机自动启动

 

         (一)、在AlertDialog的自定义布局里增加CheckBox,

    <CheckBox

        android:id="@+id/checkBox_boot"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="开机自动启动" /> 

    (二)、采用SharedPreferences保存配置

         CheckBox checkBox_boot = (CheckBox)dialogLayout.findViewById(R.id.checkBox_boot);

         SharedPreferences sharedPref =   MainActivity.this.getSharedPreferences("oaapp",Context.MODE_PRIVATE);

         Editor editor = sharedPref.edit();   

         editor.putBoolean("boot", checkBox_boot.isChecked());

         editor.commit();

        

         (三)、修改BootBroadcastReceiver的onReceive方法:

                   SharedPreferences sharedPref = arg0.getSharedPreferences("oaapp",

                                     Context.MODE_PRIVATE);

                   boolean boot = sharedPref.getBoolean("boot", true);

                   if (boot) {

                    Intent intent = new Intent(arg0, MainActivity.class);

                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    arg0.startActivity(intent);

                   }

使用KeyguardManager解除屏幕锁定

    当开机时,如果设置了屏幕锁定,只有解除锁定才能看到启动的应用。这里再增加代码解除屏幕锁定:

    (一)、在BootBroadcastReceiver的onReceive里增加

        //解除屏幕锁定

             KeyguardManager keyguardManager = (KeyguardManager)arg0.getSystemService(arg0.KEYGUARD_SERVICE);

                KeyguardLock keyguardLock = keyguardManager.newKeyguardLock(arg0.KEYGUARD_SERVICE);                

                keyguardLock.disableKeyguard();

    (二)、在manifest.xml里增加权限

         <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

原文地址:https://www.cnblogs.com/soarwell/p/3788451.html