• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 224  文章 - 41  trackbacks - 0
            <2008年11月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            享受編程

            常用鏈接

            留言簿(11)

            隨筆分類(159)

            隨筆檔案(224)

            文章分類(2)

            文章檔案(4)

            經(jīng)典c++博客

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            原文地址:http://yueguc.javaeye.com/blog/620410
            背景知識:當(dāng)Android啟動時,會發(fā)出一個系統(tǒng)廣播,內(nèi)容為ACTION_BOOT_COMPLETED,它的字符串常量表示為 android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到這個消息,再啟動之即可。記住,Android框架說:Don't call me, I'll call you back。我們要做的是做好接收這個消息的準(zhǔn)備,而實現(xiàn)的手段就是實現(xiàn)一個BroadcastReceiver。

            代碼解析:

            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);
                }
            }

            這段代碼很簡單,當(dāng)Activity啟動時,創(chuàng)建一個TextView,用它顯示"Hello. I started!"字樣。

            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);
              }
            }

            }

            該類派生自BroadcastReceiver,覆載方法onReceive中,檢測接收到的Intent是否符合BOOT_COMPLETED,如果符合,則啟動SayHello那個Activity。

            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" />
               </intent-filter>
              </receiver>
                </application>
                <uses-sdk android:minSdkVersion="3" />

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

            </manifest>

            注意其中粗體字那一部分,該節(jié)點向系統(tǒng)注冊了一個receiver,子節(jié)點intent-filter表示接收 android.intent.action.BOOT_COMPLETED消息。不要忘記配置 android.permission.RECEIVE_BOOT_COMPLETED權(quán)限。

            延伸思考:在多數(shù)情況下,要自動運行的不是有界面的程序,而是在后臺運行的service。此時,就要用startService來啟動相應(yīng)的 service了。

            代碼下載
            posted on 2010-05-11 10:18 漂漂 閱讀(1173) 評論(0)  編輯 收藏 引用 所屬分類: android 開發(fā)
            久久综合久久久| 精产国品久久一二三产区区别 | 2021国内久久精品| 一本色综合久久| 99久久超碰中文字幕伊人| 91超碰碰碰碰久久久久久综合| AAA级久久久精品无码区| 色综合久久夜色精品国产| 久久精品国产亚洲av高清漫画| 精品视频久久久久| 久久青青草原精品国产| 午夜精品久久久久成人| 免费观看久久精彩视频| 国产精品亚洲综合久久| 国产精品激情综合久久| 久久久无码精品亚洲日韩蜜臀浪潮 | 中文字幕久久精品| 国产99精品久久| 久久精品国产久精国产果冻传媒| 久久久久久久尹人综合网亚洲| 久久夜色精品国产| 国产精品日韩深夜福利久久 | 久久久久国产一级毛片高清版| 亚洲国产一成久久精品国产成人综合 | 亚洲级αV无码毛片久久精品| 国产成人综合久久精品尤物| 久久久无码人妻精品无码| 综合久久精品色| 亚洲国产精品狼友中文久久久| 九九久久精品无码专区| 久久se精品一区精品二区| 欧美va久久久噜噜噜久久| 国产成人久久精品一区二区三区| 色欲综合久久躁天天躁| 久久久免费观成人影院| 久久久受www免费人成| 久久精品无码一区二区日韩AV| 一级做a爰片久久毛片人呢| 色噜噜狠狠先锋影音久久| 91久久九九无码成人网站 | 久久国产精品成人影院|