这是个很简单的过程,这个不像pc机得开机启动,要有精确的内存地址,这里所谓的简单是跟pc机得开机启动相比。android系统的宗旨说的是,不要来找我,我会来找你。
Activity好像是应用程序的眼睛,用眼神与用户交流而用户则用手指头告诉Activity我想要什么。BroadcastReceiver好比android程序的耳朵,接受来自各方的Intent。Service好比android应用程序的手,正确完成耳朵接收到得消息,最后,我 来 组 成 头 部。虽然本人只对Activity是眼睛这句话有深刻的理解,剩下两句没有第一句感觉那么精辟,但是直觉三句话都很精辟所以从书上抄下来了。
过程是这样的,当所有的android系统服务启动完成以后,会像发传单一样像外面散布消息,这个过程就是广播,我们需要做的就是去捕捉这个系统启动完成的消息,捕捉到这个消息以后,该启动Activity就启动Activity,该启动服务就启动服务,最好的办法就是实践。
有三个地方值得注意的,配置AndroidManifest.xml,第一个,继承自Broadcast的自定义类需在xml文件中注册,第二个,继承自Service的自定义类需在xml文件中注册,第三个,捕捉一个系统启动的广播消息。贴代码 开始
这个是接收广播的类,
- package opq.broadcast;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- public class BootBroadCastReceiver extends BroadcastReceiver{
- public static final String ACTION = "android.intent.action.BOOT_COMPLETED";
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- if (intent.getAction().equals(ACTION)) {
- Log.d("TAG","ok");
- Intent myIntent=new Intent();//intent对象 用于启动服务
- myIntent.setAction("opq.service.BootService");
- context.startService(myIntent);//开机 启动服务
- }
- }
- }
- package opq.service;
-
- import opq.boot.R;
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.IBinder;
- import android.provider.MediaStore.Audio.Media;
- import android.util.Log;
-
- public class BootService extends Service{
- MediaPlayer mediaPlayer;
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- Log.d("TAG","BootService onCreate");
- mediaPlayer=MediaPlayer.create(getApplicationContext(), R.raw.anhao);
-
- super.onCreate();
- }
- @Override
- public void onStart(Intent intent, int startId) {
- // TODO Auto-generated method stub
- Log.d("TAG","BootService onStart");
- mediaPlayer.start();
- mediaPlayer.setLooping(false);
- super.onStart(intent, startId);
- }
- }
最后吧xml文件贴出来:在xml里面本人就把类名配错过,找了半天才看出来的,这玩意测试的过程比较麻烦,每次都要重启模拟器
java代码:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="opq.boot"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="7" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".BootmusicActivity"
- 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="opq.broadcast.BootBroadCastReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- </intent-filter>
- </receiver>
- <service android:name="opq.service.BootService">
- <intent-filter>
- <action android:name="opq.service.BootService"/>
- </intent-filter>
- </service>
- </application>
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- </manifest>
原来,在3.1之后,系统的package manager增加了对处于“stopped state”应用的管理,这个stopped和Activity生命周期中的stop状态是完全两码事,指的是安装后从来没有启动过和被用户手动强制停止的应用,与此同时系统增加了2个Flag:FLAG_INCLUDE_STOPPED_PACKAGES和FLAG_EXCLUDE_STOPPED_PACKAGES ,来标识一个intent是否激活处于“stopped state”的应用。当2个Flag都不设置或者都进行设置的时候,采用的是FLAG_INCLUDE_STOPPED_PACKAGES的效果。
有了上面的新机制之后,google觉得给所有的广播intent默认加上FLAG_EXCLUDE_STOPPED_PACKAGES会非常的Cooooool,能在一定程度上避免流氓软件、病毒啊干坏事,还能提高效率,就导致了本文题目中说的问题,RECEIVE_BOOT_COMPLETED广播如果用户没有运行过应用,就不会响应了。
不过google还是留了点余地,允许应用和后台服务通过给广播intent设置FLAG_INCLUDE_STOPPED_PACKAGES来唤醒处于“stopped state”的程序,也就是用户自己写的广播intent可以控制这个机制,但是系统自带的广播intent,由于不能修改,就只能接受这个现实了。
在3.1的更新文档中,能够找到上述修改的说明:http://developer.android.com/sdk/android-3.1.html#launchcontrols