Android 程序开机自启动的设置

本文介绍了如何使Android程序在开机时自动启动。关键步骤包括申请相关权限,创建广播接收器,并确保使用正确的安装包名。详细步骤帮助开发者实现应用程序的开机启动功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单的就是接收开机的广播,然后启动程序

0:首先是权限:

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

1:然后是创建广播接受者;

要注意两点:安装包名,这个可以在清单文件中找到,

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.wbm.app.tvad">

还有就是启动的时候必须要设置一个Flag:不然会报错:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

package com.wbm.app.receiver;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.wbm.app.tvad.LoginActivity;

import java.util.List;

public class TvStartReceiver extends BroadcastReceiver {
    public TvStartReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
            // TODO: This method is called when the BroadcastReceiver is receiving
            // an Intent broadcast.
    //        throw new UnsupportedOperationException("Not yet implemented");
            Log.d("kodulf", "TV 启动了啊");

        String action = intent.getAction();
        if("android.intent.action.BOOT_COMPLETED".equals(action)){
            Log.d("151217MY", "Started...");
            //Intent login = new Intent(context, LoginActivity.class);
            //service.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //context.startActivity(service);
            //context.startActivity(login);
            Toast.makeText(context,"启动了",Toast.LENGTH_LONG).show();
            openApp(context,"com.wbm.app.tvad");
        }


    }

    private void openApp(Context context, String packageName) {

        PackageInfo pi = null;
        PackageManager pm = context.getPackageManager();
        try {
            pi = pm.getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(pi.packageName);

        List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);

        ResolveInfo ri = apps.iterator().next();
        if (ri != null ) {
            packageName = ri.activityInfo.packageName;
            String className = ri.activityInfo.name;

            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);

            ComponentName cn = new ComponentName(packageName, className);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(cn);
            context.startActivity(intent);
        }
    }
}

清单文件中的设置:

<receiver
            android:name="com.wbm.app.receiver.TvStartReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>

        </receiver>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值