美文网首页
安卓开机自动启动app

安卓开机自动启动app

作者: 冬小满 | 来源:发表于2017-01-22 14:42 被阅读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>
    

    经过以上步骤就可以了

    相关文章

      网友评论

          本文标题:安卓开机自动启动app

          本文链接:https://www.haomeiwen.com/subject/wtkobttx.html