• 安卓开发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" />

  • 相关阅读:
    Sonar系列:IDEA集成SonarLint(二)
    Jenkins 安装部署全过程
    使用Docker搭建Sonarqube
    Sonar系列:SonarQube+SonarScanner 最全安装步骤(一)
    微信小程序反编译解包教程
    一张图告诉你,如何攻击Java Web应用
    windows server2019共享文件配额报错The quota limit is 10240.00 MB and the current usage is 10213.39 MB (99% of limit)的处理
    jenkins使用ssh remote插件执行shell后无法退出的问题处理
    基于docker镜像centos:7 镜像制作自定义的centos及tomcat/php/nginx镜像
    centos7.9环境基于docker和pipeline构建jenkins的ci平台
  • 原文地址:https://www.cnblogs.com/soarwell/p/3788451.html
Copyright © 2020-2023  润新知