首页 > 代码库 > 开机启动(无序广播)

开机启动(无序广播)

package com.example.start;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
     if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
         System.out.println("手机开机了!!");
         //开启MainActivity
         Intent intent2 = new Intent(context, MainActivity.class);
         //把MainActivity放到任务栈里
         intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(intent2);
      }
   }
}

清单文件中:

       <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
       <receiver android:name="com.example.start.StartReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

 

开机启动(无序广播)