Android应用程序显示欢迎画面并开机自启动

整理一下学来的两种方法:

显示欢迎画面方法如下:

首先新建一个Activity作为欢迎画面,将Manifest.xml中本来在Mainctivity注册文件下的

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

放到新注册的Activity下,使其首先启动。

Activity的代码如下:

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
//欢迎界面
@SuppressLint("HandlerLeak")
public class WelcomeActivity extends Activity{
    
    private static final String S = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏        
        setContentView(R.layout.welcome);
        //启动线程
        Thread mt = new Thread(mThread);
        mt.start();
    }
    

    private Handler mHandler = new Handler(){
        
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);

            if((String)msg.obj == S) {
                //跳转
                Intent intent = new Intent();
                intent.setClass(WelcomeActivity.this, MainActivity.class);
                WelcomeActivity.this.startActivity(intent); 
                finish();
            }
        }
    };
    
    Runnable mThread = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Message msg = mHandler.obtainMessage();
            //延时3秒
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            msg.obj = S;
            mHandler.sendMessage(msg);
        }
        
    };

}

首先使窗口全屏,显示画面,然后打开一个线程,在线程中延时3秒发送一个消息,在消息处理中跳转到Mainctivity并且关闭WelcomeActivity。这样就实现了开机欢迎画面出现3秒的效果。

使程序自启动的方法如下:

首先增加接收广播机制,用来接收系统启动时的广播,接收到之后启动程序:

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

public class BootBroadcastReceiver extends BroadcastReceiver {
    static final String action_boot="android.intent.action.BOOT_COMPLETED";
 
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(action_boot)){
            Intent ootStartIntent=new Intent(context,WelcomeActivity.class);
            ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(ootStartIntent);
        }
 
    }
 
}

添加权限:

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

注册:

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

注:这种系统自启动的方法完全在应用中实现,要等系统完全启动出现主界面之后再启动,就跟360安全卫士这样的开机启动功能。如果使自己的应用代替系统自己的Launcher,开机直接进入程序或者甚至自定义开机画面,则要在Android源码中修改。

原文地址:https://www.cnblogs.com/lcyty/p/2996541.html