andriod应用程序开机自动启动

来源:互联网 发布:js将字符串转为数组 编辑:程序博客网 时间:2024/06/12 20:13

开机自动启动,实现很简单:步骤如下


1、在AndroidManifest.xml中注册receiver

  1. <receiver android:name=".BootReceiver"  
  2.     android:label="@string/app_name">  
  3.     <intent-filter>  
  4.   
  5.         <action android:name="android.intent.action.BOOT_COMPLETED"/>  
  6.           
  7.     </intent-filter>  
  8. </receiver>  

2、添加权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> 

3、AutoStart.java

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


public class AutoStart extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";  
@Override
public void onReceive(Context context, Intent intent) {
 if (intent.getAction().equals(ACTION)){  
  Intent sayHelloIntent=new Intent(context,MainActivity.class);  
  sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
 
  context.startActivity(sayHelloIntent);  
 }  
}  
/*if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
Log.d("BootReceiver", "system boot completed");  
Intent intent2=new Intent(context,BeginActivity.class);
intent2.setAction("android.intent.action.MAIN");
intent2.addCategory("android.intent.category.LAUNCHER"); 
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);*/
}


注意:

  AutoStart .java 要 和MainActivity.java放在一个包下!

或者在注册AutoStartr 的时候,对命名的相应改动

资源下载地址:http://download.csdn.net/detail/chixinwuxue/8253125 

0 0