码迷,mamicode.com
首页 > 移动开发 > 详细

Android实现开机自启动(一)——实现接收开机广播

时间:2014-07-27 22:03:39      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

API19也就是4.4版本,用的是模拟器AVD。

操作方法如下:

1.安装apk,手动启动一次。

2.从window选项中启动AVD,不要run as application启动。

代码很简单,也贴上来给新手看看。

分三个步骤:

1.new 一个class,重写onReceive方法

2.在manifest中注册receiver。

3.授予接收系统广播的权限(3.1开始必须授予权限才可以接收系统广播)

代码如下:

bubuko.com,布布扣
 1 package com.example.testreceiver;
 2 
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 import android.util.Log;
 7 
 8 public class MyReceiver extends BroadcastReceiver{
 9     String Tag = "Tag";
10     @Override
11     public void onReceive(Context context, Intent intent) {
12         // TODO Auto-generated method stub
13         Log.w(Tag, "you have received system msg");
14         Log.i(Tag,"you have received 123system msg");
15         
16     }
17 
18 }
View Code

manifest中的配置如下

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.testreceiver"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="19"
 9         android:targetSdkVersion="21" />
10 
11     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
12 
13     <application
14         android:allowBackup="true"
15         android:icon="@drawable/ic_launcher"
16         android:label="@string/app_name"
17         android:theme="@style/AppTheme" >
18         <activity
19             android:name=".MainActivity"
20             android:label="@string/app_name" >
21             <intent-filter>
22                 <action android:name="android.intent.action.MAIN" />
23 
24                 <category android:name="android.intent.category.LAUNCHER" />
25             </intent-filter>
26         </activity>
27 
28         <receiver android:name="com.example.testreceiver.MyReceiver" >
29             <intent-filter>
30                 <action android:name="android.intent.action.BOOT_COMPLETED" >
31                 </action>
32             </intent-filter>
33         </receiver>
34     </application>
35 
36 </manifest>
View Code

只需要在自己的文件中注册一个receiver,以及写一个权限声明即可。

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

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

Android实现开机自启动(一)——实现接收开机广播

标签:

原文地址:http://www.cnblogs.com/lyc602/p/3871275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!