• <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
            <2011年3月>
            272812345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            享受編程

            常用鏈接

            留言簿(11)

            隨筆分類(159)

            隨筆檔案(224)

            文章分類(2)

            文章檔案(4)

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

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

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

            這段代碼很簡單,當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權限。

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

            代碼下載
            posted on 2010-05-11 10:18 漂漂 閱讀(1181) 評論(0)  編輯 收藏 引用 所屬分類: android 開發(fā)
            99久久99这里只有免费的精品| 久久只有这里有精品4| 狠狠色综合网站久久久久久久高清 | 久久人妻AV中文字幕| 久久人人爽人人爽人人片AV不 | 久久一日本道色综合久久| 久久91精品久久91综合| 久久婷婷五月综合成人D啪| 久久91亚洲人成电影网站| 久久九九兔免费精品6| 国产L精品国产亚洲区久久| 久久亚洲精品成人无码网站| 精品国产91久久久久久久a| 国产成人精品久久一区二区三区 | 国产精品久久久久a影院| 久久精品国产只有精品2020| 久久精品极品盛宴观看| 久久91这里精品国产2020| 精品久久8x国产免费观看| 久久99久国产麻精品66| 伊人久久无码精品中文字幕| 国产免费久久精品丫丫| 亚洲成色999久久网站| 国产精品福利一区二区久久| 囯产精品久久久久久久久蜜桃| 欧美麻豆久久久久久中文| 亚洲国产天堂久久综合网站 | 久久国产精品99久久久久久老狼| 亚洲色婷婷综合久久| 亚洲午夜久久久影院| 7777精品伊人久久久大香线蕉| 久久久久人妻一区精品果冻| 国产精品久久久99| 久久99精品久久久久久水蜜桃| 91精品国产91久久久久久| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 伊人久久精品无码av一区| 久久婷婷五月综合色奶水99啪| 2020久久精品亚洲热综合一本| 午夜精品久久久久久影视riav| 久久久久亚洲国产|