通过开机广播(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))

 

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

posted on 2018-03-28 08:59  maogefff  阅读(799)  评论(0编辑  收藏  举报

导航