Android开机启动Activity或Service
jopen
10年前
- package com.billhoo.study;
- import android.app.Activity;
- import android.os.Bundle;
- public class BootTestActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- }
- package com.billhoo.study;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- public class BootCompletedReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
- {
- Intent newIntent = new Intent(context, BootTestActivity.class);
- newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //注意,必须添加这个标记,否则启动会失败
- context.startActivity(newIntent);
- }
- }
- }
- <receiver android:name=".BootCompletedReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- </intent-filter>
- </receiver>
- <!-- permissions -->
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
转自http://billhoo.blog.51cto.com/2337751/761230