<Android+Java>设置开机启动---开机解锁并直接进入应用

来源:互联网 发布:视频加背景音乐软件 编辑:程序博客网 时间:2024/05/22 08:00

   使用Java在Android环境下开发应用,设置该应用为开机自动运行,使用一个简单的Hello,World!程序进行测试。

 1.首先写一个简单的只显示一行文字的代码如下,类SayHello.class:

package com.example.bootstartdemo;import android.app.Activity;import android.os.Bundle;import android.view.*;public class SayHello extends Activity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,                 WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); setContentView(R.layout.activity_main);}}
创建一个只显示一行文字的Activity,并将开机锁定功能关闭
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,                 WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
对应的XML代码如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.bootstartdemo.MainActivity"    tools:ignore="MergeRootFrame" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <TextView            android:id="@+id/textView1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="0.70"            android:text="Hello,Android,this is Shine!" />    </LinearLayout></FrameLayout>


2.接收广播消息类 BroadcastReceiver.class
package com.example.bootstartdemo;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class BootBroadcastReceiver extends BroadcastReceiver{static final String ACTION = "android.intent.action.BOOT_COMPLETED";@Overridepublic void onReceive(Context context,Intent intent){if(intent.getAction().equals(ACTION)){Intent sayHelloIntent = new Intent(context,SayHello.class);sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(sayHelloIntent);}}}
该类继承自BroadcastReceiver,并重写方法onReceive。使用Intent判断是否是进行的动作,即开机完成BOOT_COMPLETED,如果是,则下一步关联到SayHello类进行处理,SayHelloIntent将上下文与SayHello类进行绑定,作为无参数的activity跳转。(Intent是一种传参方式。)


3.配置文件:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.bootstartdemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="15" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.bootstartdemo.SayHello"            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=".BootBroadcastReceiver" >            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED" />            </intent-filter>        </receiver>    </application>     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> </manifest>
其中

<receiver android:name=".BootBroadcastReceiver" >            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED" />            </intent-filter>        </receiver>
表示接受者为BootBroadcastReceiver可接收的动作是BOOT_COMPLETED,根据该配置,在进行该动作时,与该类关联。要配置
android.permission.RECEIVE_BOOT_COMPLETED

权限,否则设置不成功。

0 0
原创粉丝点击