android程序开机自动启动

MainActivity.java

package testt.faith;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	/**
	 * 按下返回键退出程序
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		MainActivity.this.finish();
		System.exit(0);
	}
}


AutoStartAnActivityBroadcastReceiver.java

package test.faith;

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

public class AutoStartAnActivityBroadcastReceiver extends BroadcastReceiver {
	/**
	 * 开机自动启动
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = "android.intent.action.MAIN";
		String category = "android.intent.category.LAUNCHER";
		Intent myIntent = new Intent(context, MainActivity.class);
		myIntent.setAction(action);
		myIntent.addCategory(category);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(myIntent);
		// start service
		// Intent s = new Intent(context, MyService.class);
		// context.startService(s);
	}

}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="test.faith"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <!-- 配置Receiver的许可,允许接收系统启动消息 -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            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,可以接收系统启动消息 -->
        <receiver android:name="AutoStartAnActivityBroadcastReceiver" >
            <intent-filter>

                <!-- 在系统启动后,这个动作被广播一次(只有一次) -->
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

 

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">BroadcastReceiver</string>
</resources>



 

你可能感兴趣的:(android程序开机自动启动)