教你开启自启动程序

本文介绍如何在Android设备上设置应用程序自启动。通过定义BroadcastReceiver并注册特定的intent action,可以在系统启动完成时自动启动指定的应用活动。

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

教你开启自启动程序! 在电脑的操作系统中都能在电脑开机后自启动一些程序,在Android平台也是可以的,那么我们如何才能做到这样的效果呢。
1、需要BroadcastReceiver
2、使用interfilter中的action:android.intent.action.BOOT_COMPLETED //当启动手机系统启动完成后就启动此Receiver

功能:当手机系统启动完成后,直接启动某个程序或者Activity,这里直接启动某个程序

一下是代码分析:

AndroidManifest.xml文件内容:



Xml代码
# <?xml version="1.0" encoding="utf-8"?>
# <manifest xmlns:android="http://schemas.android.com/apk/res/android"
# package="cc.androidos.sms"
# android:versionCode="1"
# android:versionName="1.0.0">
# <application android:icon="@drawable/icon" android:label="@string/app_name">
# <activity android:name=".StartUp"
# 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=".BootReceiver">
# <intent-filter>
# <action android:name="android.intent.action.BOOT_COMPLETED" />
# </intent-filter>
# </receiver>
# <service android:name=".StartService"/>
# </application>
# </manifest>

# <?xml version="1.0" encoding="utf-8"?>
# <manifest xmlns:android="http://schemas.android.com/apk/res/android"
# package="cc.androidos.sms"
# android:versionCode="1"
# android:versionName="1.0.0">
# <application android:icon="@drawable/icon" android:label="@string/app_name">
# <activity android:name=".StartUp"
# 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=".BootReceiver">
# <intent-filter>
# <action android:name="android.intent.action.BOOT_COMPLETED" />
# </intent-filter>
# </receiver>
# <service android:name=".StartService"/>
# </application>
# </manifest> 要启动的Activity类:



Java代码
# package cc.androidos.sms;
# import android.app.Activity;
# import android.os.Bundle;
# public class StartUp extends Activity {
# /** Called when the activity is first created. */
# @Override
# public void onCreate(Bundle savedInstanceState) {
# super.onCreate(savedInstanceState);
# setContentView(R.layout.main);
# }
# }

# package cc.androidos.sms;
# import android.app.Activity;
# import android.os.Bundle;
# public class StartUp extends Activity {
# /** Called when the activity is first created. */
# @Override
# public void onCreate(Bundle savedInstanceState) {
# super.onCreate(savedInstanceState);
# setContentView(R.layout.main);
# }
# } Receiver类:系统启动后接受信息的类



Java代码
# package cc.androidos.sms;
# import android.app.Activity;
# import android.app.PendingIntent;
# import android.content.BroadcastReceiver;
# import android.content.Context;
# import android.content.Intent;
# import android.net.Uri;
# import android.util.Log;
# public class BootReceiver extends BroadcastReceiver
# {
# @Override
# public void onReceive( Context context, Intent intent )
# {
# if(intent.getAction().equals( Intent.ACTION_BOOT_COMPLETED )){
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Intent i = new Intent(context,StartUp.class);
# i.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
# //使用Receiver直接启动Activity时候需要加入此flag,否则系统会出现异常
# context.startActivity( i );
#
# }
# }
# }

# package cc.androidos.sms;
# import android.app.Activity;
# import android.app.PendingIntent;
# import android.content.BroadcastReceiver;
# import android.content.Context;
# import android.content.Intent;
# import android.net.Uri;
# import android.util.Log;
# public class BootReceiver extends BroadcastReceiver
# {
# @Override
# public void onReceive( Context context, Intent intent )
# {
# if(intent.getAction().equals( Intent.ACTION_BOOT_COMPLETED )){
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );
# Intent i = new Intent(context,StartUp.class);
# i.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
# //使用Receiver直接启动Activity时候需要加入此flag,否则系统会出现异常
# context.startActivity( i );
#
# }
# }
# } 第一个运行完成后,关闭手机模拟器或者手机,然后启动手机操作系统,启动完成后StartUp Activity会自动运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值