• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

cynchanpin

  • 博客园
  • 联系
  • 订阅
  • 管理

View Post

Android开发之使用BroadcastReceiver实现开机自己主动启动(源码分享)

      上一节已经介绍过BroadcastReceiver实现实时监听电量的功能,这节就来介绍一下假设实现开机自己主动启动的功能。这个比监听电量还简单不少

(1)在清单文件注冊权限

    

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

(2)实现  BroadcastReceiver接口

package com.example.g04_broadcastreciver04;

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

public class BootComplete extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
         Intent intent2=new Intent(context,MainActivity.class);
         intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(intent2);
         
	}

}

(3)在清单文件注冊Receiver

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

                <category android:name="android.intent.category.HOME" >
                </category>
            </intent-filter>
        </receiver>


posted on 2017-06-29 16:44  cynchanpin  阅读(808)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3