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

[java] view plaincopyprint?

  1. package com.ghstudio.BootStartDemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.TextView;  
  6.   
  7. public class SayHello extends Activity {  
  8.   
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.   
  13.   
  14.         TextView tv = new TextView(this);  
  15.         tv.setText("Hello. I started!");  
  16.   
  17.   
  18.         setContentView(tv);  
  19.     }  
  20. }  

package com.ghstudio.BootStartDemo;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.widget.TextView;</p><p>public class SayHello extends Activity {</p><p> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);</p><p> TextView tv = new TextView(this);<br /> tv.setText("Hello. I started!");</p><p> setContentView(tv);<br /> }<br />}</p><p>

 

這段代碼很簡單,當Activity啟動時,建立一個TextView,用它顯示"Hello. I started!"字樣。

2、接收廣播訊息:BootBroadcastReceiver.java

[java] view plaincopyprint?

  1. package com.ghstudio.BootStartDemo;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6.   
  7. public class BootBroadcastReceiver extends BroadcastReceiver {  
  8.   
  9.  static final String ACTION = "android.intent.action.BOOT_COMPLETED";  
  10.    
  11.  @Override  
  12.  public void onReceive(Context context, Intent intent) {  
  13.     
  14.   if (intent.getAction().equals(ACTION)){  
  15.    Intent sayHelloIntent=new Intent(context,SayHello.class);  
  16.    sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  17.   
  18.    context.startActivity(sayHelloIntent);  
  19.   }  
  20.  }  
  21.   
  22. }  

package com.ghstudio.BootStartDemo;</p><p>import android.content.BroadcastReceiver;<br />import android.content.Context;<br />import android.content.Intent;</p><p>public class BootBroadcastReceiver extends BroadcastReceiver {</p><p> static final String ACTION = "android.intent.action.BOOT_COMPLETED";</p><p> @Override<br /> public void onReceive(Context context, Intent intent) {</p><p> if (intent.getAction().equals(ACTION)){<br /> Intent sayHelloIntent=new Intent(context,SayHello.class);<br /> sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);</p><p> context.startActivity(sayHelloIntent);<br /> }<br /> }</p><p>}</p><p>

 

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

3、設定檔:AndroidManifest.xml

[xhtml] view plaincopyprint?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.ghstudio.BootStartDemo"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".SayHello"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.   <receiver android:name=".BootBroadcastReceiver">  
  15.   <intent-filter>  
  16.     <action android:name="android.intent.action.BOOT_COMPLETED" />  
  17.    </intent-filter>  
  18.   </receiver>  
  19.     </application>  
  20.     <uses-sdk android:minSdkVersion="3" />  
  21.   
  22.    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>  
  23.   
  24. </manifest>   

<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="com.ghstudio.BootStartDemo"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".SayHello"<br /> android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /> <receiver android:name=".BootBroadcastReceiver"><br /> <intent-filter><br /> <action android:name="android.intent.action.BOOT_COMPLETED" /><br /> </intent-filter><br /> </receiver><br /> </application><br /> <uses-sdk android:minSdkVersion="3" /></p><p> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission></p><p></manifest><br />

 

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

完成後,編譯出apk包,安裝到模擬器或手機中。關機,重新開機。

運行:

 

延伸思考:在多數情況下,要自動啟動並執行不是有介面的程式,而是在後台啟動並執行service。此時,就要用startService來啟動相應的service了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.