您的位置:首页 > 移动开发

android 开机自动启动某个APP运行界面

2016-09-27 11:32 441 查看
项目文件目录:



只需如下两个步骤即可:

步骤一: 添加广播:

package com.broadcast;

import com.example.signaturedemo1.MainActivity;

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

public class BootBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

//MainActivity就是开机显示的界面
Intent mBootIntent = new Intent(context, MainActivity.class);

//下面这句话必须加上才能开机自动运行app的界面
mBootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mBootIntent);
}

}

步骤二: AndroidManifest.xml中加入权限和配置相关信息
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

在<application>代码块中加入以下代码:(注意广播的android:name的值要写你对应广播的位置,本文的BootBroadcastReceiver.class在项目目录com.broadcast下,所以设置为:android:name="com.broadcast.BootBroadcastReceiver")

<!--开机广播接受者-->
<receiver android:name="com.broadcast.BootBroadcastReceiver">
<intent-filter>
<!--注册开机广播地址-->
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

既可以打开APP主界面了,附上MainActivity主代码:
package com.example.signaturedemo1;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);<span style="font-family: 'Microsoft YaHei'; white-space: pre-wrap;">} </span><span style="font-family: 'Microsoft YaHei'; white-space: pre-wrap;">}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息