Android|Android 程序开机自启动

编写一个开机广播,并在清单文件中配置

package com.retart.start; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class BootBroadcastReceiver extends BroadcastReceiver { static final String action_boot = "android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction().equals(action_boot)) { Intent bootStartIntent = new Intent(context, MainActivity.class); bootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(bootStartIntent); } }}

MainActivity
package com.retart.start; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.util.Log; public class MainActivity extends Activity { private static final String TAG = "starttest"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "onCreate0"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}

清单文件:注册广播


【Android|Android 程序开机自启动】

    推荐阅读