Service 开机接受广播,启动服务深入探究

   2016-10-17 0
核心提示:概述Android 4.0 (其实在3.1的版本中Google已经给出了说明)无法接收开机广播的问题本身是因为,如果应用程序安装上始终没有被打开过,那么在Android启动时,该应用无法接收到开机时的系统广播android.permission.RECEIVE_BOOT_COMPLETED。2.3+系列的版本是可

概述

Android 4.0 (其实在3.1的版本中Google已经给出了 说明 )无法接收开机广播的问题本身是因为,如果应用程序安装上始终没有被打开过,那么在Android启动时,该应用无法接收到开机时的系统广播android.permission.RECEIVE_BOOT_COMPLETED。

2.3+系列的版本是可以的,不过太老了,就不提了.

Launch controls on stoppedapplications

Starting from Android 3.1, the system’s package manager keeps track ofapplications that are in a

stopped state and provides a means of controllingtheir launch from

background processes and other applications.

Note that an application’s stopped state is not the same as an

Activity’sstopped state. The system manages those two stopped states

separately.

The platform defines two new intent flags that let a sender

specifywhether the Intent should be allowed to activate components in

stoppedapplication.

FLAG_INCLUDE_STOPPED_PACKAGES —Include intent filters of stopped

applications in the list of potential targetsto resolve against.

FLAG_EXCLUDE_STOPPED_PACKAGES —Exclude intent filters of stopped

applications from the list of potentialtargets. When neither or both

of these flags is defined in an intent, the defaultbehavior is to

include filters of stopped applications in the list ofpotential

targets.

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGESto all

broadcastintents. It does this to prevent broadcasts from background

services frominadvertently or unnecessarily launching components of

stoppped applications.A background service or application can override

this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to

broadcastintents that should be allowed to activate stopped

applications.

Applications are in a stopped state when they are first installed but

are notyet launched and when they are manually stopped by the user (in

ManageApplications).

对应翻译

启动停止程序控制

从Android 3.1开始,系统的软件包管理器跟踪处于停止状态(stopped state)的应用程序,控制其启动后台进程和其他应用程序提供了一种手段。

需要注意的是应用程序的停止状态(stopped state)和Activity的停止状态是不一样的。该系统可以分别管理这两种停止状态。

该平台定义了两个新的Intent的Flag,控制发送者指定的Intent是否应该被允许激活停止的应用程序的组件。

FLAG_INCLUDE_STOPPED_PACKAGES -包括在停止的应用程序列表中。

FLAG_EXCLUDE_STOPPED_PACKAGES -排除在停止的应用程序列表中。

当两个Flag都不设置或都设置的时候,默认操作是FLAG_INCLUDE_STOPPED_PACKAGES。

请注意,系统会将FLAG_EXCLUDE_STOPPED_PACKAGES添加到所有的广播Intent中去。它这样做是为了防止广播无意中的或不必要地开展组件的stoppped应用程序的后台服务。后台服务或应用程序可以通过向广播Intent添加FLAG_INCLUDE_STOPPED_PACKAGES标志来唤醒处于停止状态(stopped state)的应用程序。

应用程序处于停止状态情况有两种:一种是他们是第一次安装,但尚未启动 ;另一种是在管理应用程序中由用户手动停止。

在4.0中android取消了无主activity运行的线程。所以开机启动的服务必须依赖于activiy。

应用程序中必须有:

<intent-filter >
                <action android:name="android.intent.action.MAIN"></action>
                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>

程序才能正常的接收开机广播。否则无法启动服务。

开机启动的服务,需要有个一Activity,单独无Activity的Service貌似行不通。

需要在真机中测试,genymotion等模拟器经验证不行。

Code

在BroadcastReceiver中,启动service和显示一个对话框主题的Activity提示服务启动成功。 真机验证,OK。

StartupReceiver

package com.turing.base.activity.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.apkfuns.logutils.LogUtils;

/**
 * 只要完成两项工作: 启动服务 和 显示一个Activity提示服务启动成功(主题设置为Dialog的形式)
 */
public class StartupReceiver extends BroadcastReceiver {
    public StartupReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        LogUtils.e("StartupReceiver  onReceive");

        // 如果是开机启动的Action
        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){

            // 启动Activity
            Intent activityIntent = new Intent(context,BootCompletedMessageAct.class);
            // 想要在Service中启动Activity,必须设置如下标志
            activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(activityIntent);

            //启动服务
            Intent serviceIntent = new Intent(context,StartupService.class);
            context.startService(serviceIntent);




        }

    }
}

StartupService

package com.turing.base.activity.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class StartupService extends Service {
    public StartupService() {
    }

    @Override
    public IBinder onBind(Intent intent) {

        return  null ;
    }


    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int on
StartCommand(Intent intent, int flags, int startId) {
        return super.on
StartCommand(intent, flags, startId);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
package com.turing.base.activity.service;

import android.app.Activity;
import android.os.Bundle;

import com.turing.base.R;

public class BootCompletedMessageAct extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_boot_completed_message);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.turing.base.activity.service.BootCompletedMessageAct">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="MyService已启动"
        />

</RelativeLayout>

AnroidManifest.xml (权限,主题为对话框的Act, Receiver,Service)

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

  <activity
            android:name=".activity.service.BootCompletedMessageAct"
            android:theme="@android:style/Theme.Dialog">
            <intent-filter>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

  <!-- 开机广播 -->
        <receiver android:name=".activity.service.StartupReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name=".activity.service.StartupService"
            android:enabled="true"
            android:exported="true"></service>

验证

部署到真机上, 第一次先运行一次,然后关机重启,再次开机,就可以看到 这个服务启动成功后,弹出的 对话框主题样式的Act了。

如果是android4.0及以上,还是需要安装并运行一次,下次开机的时候才能实现接受广播,启动服务。而像2.3之类相对低的版本安装好了之后下次启动,就能直接获取并处理系统的开机广播,不需要先运行一次。

 
标签: 安卓开发
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 安卓中通知功能的具体实现
    安卓中通知功能的具体实现
    通知[Notification]是Android中比较有特色的功能,当某个应用程序希望给用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知实现。使用通知的步骤1、需要一个NotificationManager来获得NotificationManager manager = (NotificationManager
    02-05 安卓开发
  • Android view系统分析-setContentView
    Android view系统分析-setContentView
    第一天上班,列了一下今年要学习的东西。主要就是深入学习Android相关的系统源代码,夯实基础。对于学习Android系统源代码,也没什么大概,就从我们平常使用最基础的东西学起,也就是从view这个切入点开始学习Android的源码,在没分析源码之前,我们有的时候
    02-05 安卓开发
  • 如何进行网络视频截图/获取视频的缩略图
    如何进行网络视频截图/获取视频的缩略图
    小编导读:获取视频的缩略图,截图正在播放的视频某一帧,是在音视频开发中,常遇到的问题。本文是主要用于点播中截图视频,同时还可以获取点播视频的缩略图进行显示,留下一个问题,如下图所示, 如果要获取直播中节目视频缩略图,该怎么做呢?(ps:直播是直
  • Android NDK 层发起 HTTP 请求的问题及解决
    Android NDK 层发起 HTTP 请求的问题及解决
    前言新的一年,大家新年快乐~~鸡年大吉!本次给大家带来何老师的最新文章~虽然何老师还在过节,但依然放心不下广大开发者,在此佳节还未结束之际,给大家带来最新的技术分享~ 事件的起因不说了,总之是需要实现一个 NDK 层的网络请求。为了多端适用,还是选择
  • Android插件化(六): OpenAtlasの改写aapt以防止资源ID冲突
    Android插件化(六): OpenAtlasの改写aapt以防
    引言Android应用程序的编译中,负责资源打包的是aapt,如果不对打包后的资源ID进行控制,就会导致插件中的资源ID冲突。所以,我们需要改写aapt的源码,以达到通过某种方式传递资源ID的Package ID,通过aapt打包时获取到这个Package ID并且应用才插件资源的命名
    02-05 安卓开发
  • Android架构(一)MVP架构在Android中的实践
    Android架构(一)MVP架构在Android中的实践
    为什么要重视程序的架构设计 对程序进行架构设计的原因,归根结底是为了 提高生产力 。通过设计是程序模块化,做到模块内部的 高聚合 和模块之间的 低耦合 (如依赖注入就是低耦合的集中体现)。 这样做的好处是使得程序开发过程中,开发人员主需要专注于一点,
    02-05 安卓开发
  • 安卓逆向系列教程 4.2 分析锁机软件
    安卓逆向系列教程 4.2 分析锁机软件
    安卓逆向系列教程 4.2 分析锁机软件 作者: 飞龙 这个教程中我们要分析一个锁机软件。像这种软件都比较简单,完全可以顺着入口看下去,但我这里还是用关键点来定位。首先这个软件的截图是这样,进入这个界面之后,除非退出模拟器,否则没办法回到桌面。上面那
    02-05 安卓开发
  • Android插件化(二):OpenAtlas插件安装过程分析
    Android插件化(二):OpenAtlas插件安装过程分析
    在前一篇博客 Android插件化(一):OpenAtlas架构以及实现原理概要 中,我们对应Android插件化存在的问题,实现原理,以及目前的实现方案进行了简单的叙述。从这篇开始,我们要深入到OpenAtlas的源码中进行插件安装过程的分析。 插件的安装分为3种:宿主启动时立
    02-05 安卓开发
  • [译] Android API 指南
    [译] Android API 指南
    众所周知,Android开发者有中文网站了,API 指南一眼看去最左侧的菜单都是中文,然而点进去内容还是很多是英文,并没有全部翻译,我这里整理了API 指南的目录,便于查看,如果之前还没有通读,现在可以好好看一遍。注意,如果标题带有英文,说明官方还没有翻
  • 使用FileProvider解决file:// URI引起的FileUriExposedException
    使用FileProvider解决file:// URI引起的FileUri
    问题以下是一段简单的代码,它调用系统的相机app来拍摄照片:void takePhoto(String cameraPhotoPath) {File cameraPhoto = new File(cameraPhotoPath);Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);takePhotoIntent.putExtra(Medi
    02-05 安卓开发
点击排行