安卓开机自动启动app

2017-01-22  本文已影响0人  冬小满

安卓开机自动启动app

1 新建一个类继承BroadcastReceiver, 监听系统的BOOT_COMPLETED

package com.zteits.parkingpay.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.zteits.parkingpay.MainActivity;

/**
 * Created by xiashuai on 10:24 2016/10/25.
 * Connection smay1227@163.com.
 */

public class BootBroadcastReceiver extends BroadcastReceiver {

   static final String ACTION = "android.intent.action.BOOT_COMPLETED";

   @Override
   public void onReceive(Context context, Intent intent) {
      if (intent.getAction().equals(ACTION)){
         Intent mainActivityIntent = new Intent(context, MainActivity.class);
         mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(mainActivityIntent);
      }
   }
}

2 在配置文件中添加权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

3 在配置文件中注册receiver

<receiver android:name=".receiver.BootBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

经过以上步骤就可以了

上一篇下一篇

猜你喜欢

热点阅读