• android四大组件之BroadcastReceiver开机启动


    和其他BroadcastReceiver一样需要配置接收的广播信息,然后配置权限


            <receiver
                android:name="com.example.kaijistart.BootReceiver" >
                <intent-filter >
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                </intent-filter>
            </receiver>

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

    然后就在BroadcastReceiver里进行intent跳转就可以了

    package com.example.kaijistart;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class BootReceiver extends BroadcastReceiver {
        public BootReceiver() {
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent it = new Intent(context,MainActivity.class);
            //设置新的任务栈,如果我们从系统图标点进去,系统会创建activity栈,
            //但是我们现在没有点击就要自己创建任务栈了
            it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(it);
            System.out.println("开机了");
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.kaijistart"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.kaijistart.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <receiver
                android:name="com.example.kaijistart.BootReceiver" >
                <intent-filter >
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                </intent-filter>
            </receiver>
        </application>
    
    </manifest>
  • 相关阅读:
    日本ネット書店一覧
    programming books
    Mathematics for the adventurous selflearner
    How to Setup SSH Passwordless Login in Linux[3 Easy Steps]
    4 ways to find which shell I'm using
    Optimized Japanese Vocabulary with Sound
    Professor Maitland Jones Jr. was dismissed from NYU
    中国汉字,日本汉字 学习Android App推荐
    使用spring的RedisTemplate操作redis队列 IT
    POM使用标签说明 IT
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/4968557.html
Copyright © 2020-2023  润新知