NewBrocastReceiver
public class NewBrocastReceiver extends BroadcastReceiver{
private String Action="android.intent.action.BOOT_COMPLETED";
//向系統註冊的行為
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Action))
{
//比對是否有攔截到action,註冊在AndroidManifet裡
Intent it = new Intent(context,MainActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
//如果是需要從開機後繼續執行Service,也可以startService;
}
}
}
要加上uses-permission,靜態註冊action,一定要註冊Receiver
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello_br"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!-- 要加上這個權限 -->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.hello_br.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 android:name="NewBrocastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
留言列表