Android app:开机自动启动app demo 源码

             Android APP:开机自动启动app demo 源码

 

 

一、Android启动完成会发出一个系统广播 ACTION_BOOT_COMPLETED,字符串常量表示为 “android.intent.action.BOOT_COMPLETED”,我们要做的是用BroadcastReceiver接收这个消息然后启动该程序。

 

二、android studio 源码:

     1、autorundemo\app\src\main\java\com\example\autorundemo\MainActivity.java

package com.example.autorundemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import java.util.List;
public class MainActivity extends AppCompatActivity {
    MyReceiver receiver ;
    IntentFilter intentFilter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // moveTaskToBack(true);
        setContentView(R.layout.activity_main);
        receiver = new MyReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.rotationscreen");
        registerReceiver(receiver,intentFilter);
       // moveTaskToBack(true);
    }
    public void HintDialog() {
     /*
        // 创建对话框对象
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();

        // 设置对话框的标题
        alertDialog.setTitle("这是一个消息对话框");
        // 设置对话框中的内容
        alertDialog.setMessage("消息");
        // 显示对话框
        alertDialog.show();
        */        AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(this);
        alertdialogbuilder.setMessage("moveTaskToBack?");
        alertdialogbuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                moveTaskToBack(true);
            }
        });

        alertdialogbuilder.setNeutralButton("CANCEL", null);
        final AlertDialog alertdialog1 = alertdialogbuilder.create();
        alertdialog1.show();
    }
 private void bring2Front()
    {
        ActivityManager activtyManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List runningTaskInfos = activtyManager.getRunningTasks(3);
        for (ActivityManager.RunningTaskInfo runningTaskInfo : runningTaskInfos)
        {
            if (this.getPackageName().equals(runningTaskInfo.topActivity.getPackageName()))
            {
                activtyManager.moveTaskToFront(runningTaskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME);
                return;
            }
        }
    }

    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(MainActivity.this,"receive a broadcast message android.intent.action.rotationscreen!",Toast.LENGTH_SHORT).show();
            HintDialog();
          //  moveTaskToBack(false);
            bring2Front();
        }
    }

}

     2、autorundemo\app\src\main\java\com\example\autorundemo\BootReceiver.java

package com.example.autorundemo;


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

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {     // boot
            Intent intent2 = new Intent(context, MainActivity.class);
//          intent2.setAction("android.intent.action.MAIN");
//          intent2.addCategory("android.intent.category.LAUNCHER");
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent2);
        }
    }
}

  3、autorundemo\app\src\main\res\layout\activity_main.xml



        

        
    

   4、autorundemo\app\src\main\AndroidManifest.xml



    
    
        
 
                

                
            
        
        
            
                

                
            
        

    
    

 

三、demo运行效果,开机自启动,背景透明,然后接受到广播(这里我用adb模拟发送广播)后让应用跑到前台运行并且弹出一个对话框可以选择是否切换到后台运行。

四、源码下载路径:

https://download.csdn.net/download/qq_37858386/12529475

你可能感兴趣的:(Android,App,Develop)