zoukankan      html  css  js  c++  java
  • 通过开机广播(broadcast)通知应用

    1. 概念

    开机的时候,系统会发送一则广播,所有有标记的应用(通过广播接收者)都会获取得到,然后可以通过广播接收者去处理一些事情,比如启动该应用,或者处理数据;

    代码:https://github.com/maogefff/Android-Test-Sample/tree/master/%E5%BC%80%E6%9C%BA%E5%B9%BF%E6%92%AD%E6%8E%A5%E6%94%B6%E7%A4%BA%E4%BE%8B

    2. 代码

    在这里做一个实验,就是开机以后,广播接收者接收到广播,然后通过广播调用activity(其实就相当于启动应用了)

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.aplex.serialselect">
    
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <!-- 添加接收开机启动广播 -->
            <receiver android:name=".MyReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <!-- <category android:name="android.intent.category.LAUNCHER"/>这里不能加这句,因为加了这句,默认开启应用就是启动广播,而不是acivity了 -->
                    <category android:name="android.intent.category.HOME" />
                </intent-filter>
            </receiver>
    
        </application>
    
    </manifest>

    MyReceiver.java

    package com.example.aplex.serialselect;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    public class MyReceiver extends BroadcastReceiver {
    
        public final static String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
    
        // 重写onReceive方法
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(ACTION_BOOT_COMPLETED)){
                // 后边的XXX.class就是要启动的服务
                Intent actIntent = new Intent(context.getApplicationContext(), MainActivity.class);
                actIntent.setAction("android.intent.action.MAIN");
                actIntent.addCategory("android.intent.category.LAUNCHER");
                actIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(actIntent);
                Log.e("TAG", "开机自动服务自动启动.....");
                // 启动应用
                Intent serIntent= new Intent(context, MainActivity.class);
                serIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startService(serIntent);
                Log.e("TAG", "开机程序自动启动.....");
            }
        }
    }

    MainActivity.java

    package com.example.aplex.serialselect;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }

    3. 效果

    修改为系统应用

    desk@desk-ubuntu:~/myandroid$ cat packages/apps/SerialSelect/app/src/main/Android.mk
    LOCAL_PATH := $(call my-dir)
    #清除所有变量
    include $(CLEAR_VARS)
    
    #不管是调试版本还是发行版本,都编译
    LOCAL_MODULE_TAGS := optional
    
    #源码目录
    LOCAL_SRC_FILES := $(call all-java-files-under, java)
    
    LOCAL_RESOURCE_DIR :=  $(LOCAL_PATH)/res
    
    #测试,我的作用是强制忽略多语言问题错误
    #LOCAL_MODULE_TAGS := tests
    #生成的APK的名字
    LOCAL_PACKAGE_NAME := SelectSerial
    #系统签名
    LOCAL_CERTIFICATE := platform
    #编译为APK应用
    include $(BUILD_PACKAGE)
    
    include $(call all-makefiles-under,$(LOCAL_PATH))

    因为这里是真机操作,显示就是一片白屏的

  • 相关阅读:
    打印sql语句方法
    PHP实现innodb的数据回滚
    安装Sublime Text 3插件的方法
    Redis常用命令速查 <第二篇>
    本地配置环境打开项目出现404/本地wampserver配置伪静态以及php.ini配置
    linux的tar命令
    PHP无限极分类详谈
    PHP常用函数及其注释
    PHP常用到的功能函数
    【转】小菜硬件杂谈 细数主板上曾出现过的插槽
  • 原文地址:https://www.cnblogs.com/maogefff/p/8029586.html
Copyright © 2011-2022 走看看