//1.androidManifest要加參數
//2.new HippoStartupIntentReceiver class
//3.new mainactivity class
Part 1.androidManifest
接著在AndroidManifest.xml裡面新加入receiver
注意:receiver放的類別名稱是BroadcastReceiver子類別的名稱。
Android在開機的時候,會送出android.intent.action.BOOT_COMPLETED這個廣播訊息,
因此只要設定好了,一開機就會執行指定的Activity。
----------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.startupintentreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.startupintentreceiver.MainActivity"
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名稱為類別名稱-->
<receiver android:name="HippoStartupIntentReceiver">
<!-- 在filter裡設定Boot_COMPLETED為要捕捉的訊息 -->
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
</application>
Part 2.new HippoStartupIntentReceiver class
接著寫一支BroadcastReceiver的子類別,並且覆寫onReceive這個方法,
----------------------------------------------------------------------------------------------------------------------
在同一個package下新增一個receiver class
//開機系統會發出android.intent.action.BOOT_COMPLETED的廣撥息訊,所以會用下列列Class
package com.example.startupintentreceiver;
import com.example.startupintentreceiver.MainActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class HippoStartupIntentReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
/*當收到receiver時,指定開啟此程式(MainActivity.class)*/
Intent mBootIntent=new Intent(context,MainActivity.class);
/*設定intent開啟為FLAG_ACTIVITY_NEW_TASK*/
mBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
/*將intent以startActivity傳送給作業系統*/
context.startActivity(mBootIntent);
}
}
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
Part 3. new mainactivity class
寫一支一般的應用程式,
package com.example.startupintentreceiver;
//開機時自動開啟程式,利用補捉,開機廣播來完成
//1.androidManifest要加參數
//2.new HippoStartupIntentReceiver class
//3.new mainactivity class
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}