zoukankan      html  css  js  c++  java
  • Android开机启动Activity或者Service方法

    这段时间在做Android的基础开发,现在有一需求是开机启动,按照网上某些博文教程做了下,始终不成功,一开机总是提示所启动的应用程序意外终止,于是参考了Android SDK doc,终于解决问题,下面把自己的经验分享给大家。

    【原理】

        当Android系统完成BOOT阶段之后,就会发送一条名为 ACTION_BOOT_COMPLETED 的广播,我们便可在一个BroadcastReceiver中捕获这条广播,然后启动我们的Activity或者Service,当然要注意的是,我们的application必须具有捕获该广播的权限,下面请看具体步骤:

    【步骤一】首先要有一个用于开机启动的Activity或者Service,这里以系统自己创建的最简单的Activity为例进行讲解。

    1. package com.billhoo.study;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5.  
    6. public class BootTestActivity extends Activity {
    7.     /** Called when the activity is first created. */
    8.     @Override
    9.     public void onCreate(Bundle savedInstanceState) {
    10.         super.onCreate(savedInstanceState);
    11.         setContentView(R.layout.main);
    12.     }
    13. }

    【步骤二】我们要编写一个BroadcastReceiver用以捕获ACTION_BOOT_COMPLETED这条广播,并在捕获之后启动我们要启动的Activity。

    注意:必须在intent中添加Intent.FLAG_ACTIVITY_NEW_TASK标记,这就是我之前老是启动失败的原因。至于为什么,我还在研究SDK doc,明白了之后就回来补上。

    1. package com.billhoo.study;
    2.  
    3. import android.content.BroadcastReceiver;
    4. import android.content.Context;
    5. import android.content.Intent;
    6.  
    7. public class BootCompletedReceiver extends BroadcastReceiver {
    8.   @Override
    9.   public void onReceive(Context context, Intent intent) {
    10.     if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
    11.     {
    12.       Intent newIntent = new Intent(context, BootTestActivity.class);
    13.       newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  //注意,必须添加这个标记,否则启动会失败
    14.       context.startActivity(newIntent);      
    15.     }      
    16.   }
    17. }

    【步骤三】在AndroidManifest.xml配置文件中注册我们的BroadcastReceiver

    1. <receiver android:name=".BootCompletedReceiver">
    2.     <intent-filter>
    3.         <action android:name="android.intent.action.BOOT_COMPLETED" />
    4.     </intent-filter>
    5. </receiver>

    【步骤四】在AndroidManifest.xml配置文件中添加允许我们捕获该广播的权限

    1. <!-- permissions -->
    2. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    之前把uses-permission 打错成permission ,结果一直提示下面这个错误:
    Permission Denial: receiving Intent { act=android.intent.action.BOOT_COMPLETED flg=0x8000010 (has extras) } to com.example.boottest/.BootCompletedReceiver requires android.permission.RECEIVE_BOOT_COMPLETED due to sender null (uid 1000)

    重启虚拟机,大功告成。

    下面附上完成的AndroidManifest.xml,以便大家理解参考

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.     package="com.billhoo.study" android:versionCode="1"
    4.     android:versionName="1.0">
    5.     <uses-sdk android:minSdkVersion="4" />
    6.  
    7.     <!-- permissions -->
    8.     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    9.  
    10.     <application android:icon="@drawable/icon" android:label="@string/app_name">
    11.         <!-- activities -->
    12.         <activity android:name=".BootTestActivity" android:label="@string/app_name">
    13.             <intent-filter>
    14.                 <action android:name="android.intent.action.MAIN" />
    15.                 <category android:name="android.intent.category.LAUNCHER" />
    16.             </intent-filter>
    17.         </activity>
    18.  
    19.         <!-- receivers -->
    20.         <receiver android:name=".BootCompletedReceiver">
    21.             <intent-filter>
    22.                 <action android:name="android.intent.action.BOOT_COMPLETED" />
    23.             </intent-filter>
    24.         </receiver>
    25.  
    26.     </application>
    27. </manifest>
  • 相关阅读:
    Vue 2.x windows环境下安装
    VSCODE官网下载缓慢或下载失败 解决办法
    angular cli 降级
    Win10 VS2019 设置 以管理员身份运行
    XSHELL 连接 阿里云ECS实例
    Chrome浏览器跨域设置
    DBeaver 执行 mysql 多条语句报错
    DBeaver 连接MySql 8.0 报错 Public Key Retrieval is not allowed
    DBeaver 连接MySql 8.0报错 Unable to load authentication plugin 'caching_sha2_password'
    Linux系统分区
  • 原文地址:https://www.cnblogs.com/cute/p/4871321.html
Copyright © 2011-2022 走看看