Android

应用开机启动

2018-10-11  本文已影响0人  瑟闻风倾

1.新建BootBroadcastReceiver类

package com.jack.workticket.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.jack.workticket.LoginActivity;

/** 
* Created by liy on 2017-04-13. 
*/
public class BootBroadcastReceiver extends BroadcastReceiver {   

 /**    
 *实现开机自动打开软件并运行   
 */    
@Override    
public void onReceive(Context context, Intent intent) {        
  Log.d("XRGPS", "BootReceiver.onReceive: " + intent.getAction());        
  System.out.println("自启动程序即将执行");       
   //LoginActivity就是开机显示的界面       
   Intent mBootIntent = new Intent(context, LoginActivity.class);       
   //下面这句话必须加上才能开机自动运行app的界面        
  mBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);       
   context.startActivity(mBootIntent);            
  }

}

2.AndroidManifest.xml中加入权限和配置相关信息

//开机启动
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!--开机自启动广播接受者-->
            <receiver android:name="com.jack.workticket.service.BootBroadcastReceiver">
                <intent-filter>
                    <!--注册开机广播地址-->
                    <action android:name="android.intent.action.BOOT_COMPLETED"></action>
                    <category android:name="android.intent.category.DEFAULT" />
                    <!--<category android:name="android.intent.category.HOME" />-->
                </intent-filter>
            </receiver>
上一篇 下一篇

猜你喜欢

热点阅读