zoukankan      html  css  js  c++  java
  • Android 开机自启动

    为了让自己的APP开机自启动,在网上也找了很多资料,最终测试程序,在这里记录一下。

    测试环境Android4.0

    一、首先创建一个广播接收者,重构其抽象方法 onReceive(Context context, Intent intent),在其中启动你想要启动 app。

    新建一个类BootBroadcastReceiver继承自BroadcastReceiver,重写onReceive方法

    public class BootBroadcastReceiver extends BroadcastReceiver {
    
    	@Override
    	public void onReceive(Context context, Intent intent) {
    
    		// TODO Auto-generated method stub
    		if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
    		{
    			Intent bootIntent = new Intent(context,MainActivity.class);
    			bootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);	
    			context.startActivity(bootIntent);
    		}
    		
    	}
    
    }
    

    二、修改AndroidManifest.xml配置文件

    在<application>节点中添加<receiver android:name="BootBroadcastReceiver" >节点,详细如下

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.android_test_atuostart.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <receiver android:name="BootBroadcastReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" >
                    </action>
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </receiver>
        </application>
    

    三、添加权限 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

  • 相关阅读:
    java枚举
    [bzoj3436]小K的农场【差分约束系统】【判负环】
    [bzoj1085][SCOI2005]骑士精神【暴力】
    [bzoj1034][ZJOI2008]泡泡堂BNB【贪心】
    [bzoj1046][HAOI2007]上升序列【dp】
    [bzoj1050][HAOI2006]旅行comf【MST】
    [bzoj1047][HAOI2007]理想的正方形【单调队列】
    [bzoj1004][HNOI2008]Cards【群论】
    [bzoj1045][HAOI2008] 糖果传递【构造】
    [bzoj4589]Hard Nim【FWT】
  • 原文地址:https://www.cnblogs.com/tianzhenyun/p/3520484.html
Copyright © 2011-2022 走看看