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代码是怎么运行的
    Java单例模式
    redis分布式锁实现
    zuul2.0
    配置ssh免密钥登陆多台从机
    Nifi-install-config
    Configure Access to Multiple Clusters
    kubernetes集群搭建(kubeadm,kubelet)
    shell 编程
    系统管理
  • 原文地址:https://www.cnblogs.com/tianzhenyun/p/3520484.html
Copyright © 2011-2022 走看看