如何在启动时启动/启动Android应用程序

我想在平板电脑启动时启动我的应用程序,以便用户启动平板电脑时首先看到的是应用程序的主要活动。
我读过有关LauncherActivity的信息,但我不知道如何使用它。
任何人都可以为我提供建议,链接或教程吗?

30秒到达战场
浏览 374回答 3
3回答

潇潇雨雨

这些代码行可能对您有帮助...步骤1:在AndroidManifest.xml中设置权限<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />步骤2:在接收器中添加此意图过滤器<receiver android:name=".BootReceiver">&nbsp; &nbsp; <intent-filter >&nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.BOOT_COMPLETED"/>&nbsp; &nbsp; </intent-filter></receiver>步骤3:现在您可以从onReceiveReceiver类的方法开始应用程序的第一个活动public class BootReceiver extends BroadcastReceiver {&nbsp; &nbsp;@Override&nbsp; &nbsp;public void onReceive(Context context, Intent intent) {&nbsp; &nbsp; &nbsp; &nbsp;Intent myIntent = new Intent(context, MainActivity.class);&nbsp; &nbsp; &nbsp; &nbsp;myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);&nbsp; &nbsp; &nbsp; &nbsp;context.startActivity(myIntent);&nbsp; &nbsp;}}

交互式爱情

如果您想在平板电脑启动时启动应用程序,则需要收听BOOT_COMPLETED操作并作出反应。BOOT_COMPLETED是一个广播动作,在系统完成引导之后,将广播一次。您可以通过创建BroadcastReceiver来收听此操作,然后在启动活动收到具有BOOT_COMPLETED操作的意图时启动启动活动。将此权限添加到清单中:<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />在您的项目中创建一个自定义BroadcastReceiver:public class MyBroadCastReceiver extends BroadcastReceiver {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onReceive(Context context, Intent intent) {&nbsp; &nbsp; &nbsp; &nbsp; if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent i = new Intent(context, MyActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startActivity(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}&nbsp;然后通过将BroadCastReceiver添加到清单中来修改清单文件:<receiver android:name=".MyBroadcastReceiver">&nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp;<action android:name="android.intent.action.BOOT_COMPLETED" />&nbsp; &nbsp; </intent-filter></receiver>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android