android應用如何開機啟動

來源:互聯網
上載者:User


有些時候,應用
需要在開機時就自動運行


例如某個自動從網上更新內容的後台
service
。怎樣實現開機自動啟動並執行應用?在撰寫本文時,聯想到高煥堂先生以
“Don''t call me, I''ll call you
back!”
總結
Android
架構,真是說到點子上了。理解這句話的含義,許多有關
Android
平台

上實現某種功能
的問題,都能迎刃而解。


使用情境:手機
開機後,自動運行程式
,在螢幕
上顯示
"Hello. I started!"
字樣。


背景知識:當
Android
啟動時,會發出一個系統
廣播,內容為
ACTION_BOOT_COMPLETED
,它的字串常量表示為
android

.intent.action.BOOT_COMPLETED


只要在程式中

捕捉

到這個訊息,再啟動之即可。記住,
Android
架構說:
Don''t call me, I''ll call you
back
。我們要做的是做好接收這個訊息的準備,而實現的手段就是實現一個
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
啟動時,建立一個
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>



注意其中粗體字那一部分,該節點向系統註冊了一個
receiver
,子節點
intent-filter
表示接收
android.intent.action.BOOT_COMPLETED
訊息。不要忘記配置
android.permission.RECEIVE_BOOT_COMPLETED
許可權。


完成後,編譯

apk
包,安裝到類比
器或手機中。關機,重新開機。

 


這就是一個廣播嘛,用一個
receiver
接受
RECEIVE_BOOT_COMPLETED
這個廣播事件,然後就開機就啟動這個
app



如果是系統進程的話,需要加入
init
設定檔來啟動,隨著
linux
啟動而啟動。

 

不是高手,在
linux kernel
裡面有個
init
檔案夾不是?編譯之後,會有
init
產生,這是
kernel
啟動的第一個使用者進程,是不是?


如果你有想要啟動的自己的進程如
service
的話,需要把自己的
service
加如這個
init
之中,這樣不就實現了開機就啟動了一進程?

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.