展会信息港展会大全

android开发之广播开机自启动小实例
来源:互联网   发布日期:2016-01-13 21:56:42   浏览:2573次  

导读:通过广播来实现开机启动activity1 创建activity2 创建广播3 在清单文件中注册具体实现:1、界面Activity:SayHello java package com ghstudio BootStartDemo; import android app Ac ...

通过广播来实现开机启动activity

1.创建activity

2.创建广播

3.在清单文件中注册

具体实现:

1、界面Activity:SayHello.java

package com.ghstudio.BootStartDemo;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class SayHello extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TextView tv = new TextView(this);

tv.setText("Hello. I started!");

setContentView(tv);

}

}

2、接收广播消息:BootBroadcastReceiver.java

package com.ghstudio.BootStartDemo;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

public class BootBroadcastReceiver extends BroadcastReceiver {

static final String ACTION = "android.intent.action.BOOT_COMPLETED";

@Override

public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals(ACTION)){

Intent sayHelloIntent=new Intent(context,SayHello.class);

sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(sayHelloIntent);

}

}

}

3、配置文件:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.ghstudio.BootStartDemo"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".SayHello"

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=".BootBroadcastReceiver">

<intent-filter>

<action android:name="android.intent.action.BOOT_COMPLETED" />

<category android:name="android.intent.category.DEFAULT"/>

</intent-filter>

</receiver>

</application>

<uses-sdk android:minSdkVersion="3" />

<uses-permission

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

</manifest>

4、完成后,编译出apk 包,安装到模拟器或手机中。关机,重新开机。

赞助本站

人工智能实验室

相关热词: 广播 开机自启动 android

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港