• <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
            <2010年6月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            享受編程

            常用鏈接

            留言簿(11)

            隨筆分類(159)

            隨筆檔案(224)

            文章分類(2)

            文章檔案(4)

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

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            原文地址:http://yueguc.javaeye.com/blog/620410
            背景知識:當(dāng)Android啟動(dòng)時(shí),會(huì)發(fā)出一個(gè)系統(tǒng)廣播,內(nèi)容為ACTION_BOOT_COMPLETED,它的字符串常量表示為 android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到這個(gè)消息,再啟動(dòng)之即可。記住,Android框架說:Don't call me, I'll call you back。我們要做的是做好接收這個(gè)消息的準(zhǔn)備,而實(shí)現(xiàn)的手段就是實(shí)現(xiàn)一個(gè)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啟動(dòng)時(shí),創(chuàng)建一個(gè)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,如果符合,則啟動(dòng)SayHello那個(gè)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é)點(diǎn)向系統(tǒng)注冊了一個(gè)receiver,子節(jié)點(diǎn)intent-filter表示接收 android.intent.action.BOOT_COMPLETED消息。不要忘記配置 android.permission.RECEIVE_BOOT_COMPLETED權(quán)限。

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

            代碼下載
            posted on 2010-05-11 10:18 漂漂 閱讀(1181) 評論(0)  編輯 收藏 引用 所屬分類: android 開發(fā)
            粉嫩小泬无遮挡久久久久久| 久久天天躁狠狠躁夜夜2020| 亚洲精品高清国产一线久久| 欧美熟妇另类久久久久久不卡 | 色欲久久久天天天综合网| 亚洲欧美成人综合久久久| 久久精品国产91久久麻豆自制| 久久se精品一区二区影院| 亚洲AV无码1区2区久久 | 久久只有这精品99| 成人久久综合网| 99精品久久久久久久婷婷 | 2021久久国自产拍精品| 欧美一级久久久久久久大片| 精品国产VA久久久久久久冰| 亚洲欧美日韩精品久久亚洲区| 久久精品国产亚洲沈樵| 日韩精品久久久肉伦网站| 精品无码久久久久久久久久| 久久久久AV综合网成人| 亚洲日本久久久午夜精品| 94久久国产乱子伦精品免费 | 亚洲va国产va天堂va久久| 久久亚洲电影| 精品久久久久中文字| 久久国产精品久久久| 国产成人久久精品区一区二区| 久久精品国产久精国产一老狼| 女人高潮久久久叫人喷水| 久久国产视频99电影| 久久精品女人天堂AV麻| 久久九色综合九色99伊人| 国产精品热久久无码av| 秋霞久久国产精品电影院| 日本一区精品久久久久影院| 国产精品视频久久久| 日韩一区二区久久久久久| 久久国产精品国产自线拍免费| 久久国产精品99久久久久久老狼| 久久99毛片免费观看不卡| 久久久久97国产精华液好用吗|