博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android4.0 开机自启动
阅读量:6992 次
发布时间:2019-06-27

本文共 2550 字,大约阅读时间需要 8 分钟。

1、Manifest.xml如下。

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android=""
 android:versionCode="1" android:versionName="1.0" package="cn.etzmico.autorun">

 <application android:icon="@drawable/icon" android:label="@string/app_name">
  <receiver android:name="cn.etzmico.BootReceiver"
   android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </receiver>
  <activity android:name="cn.etzmico.AutoRun" android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>

 </application>

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>  设置可以接受自启动的权限。

</manifest>

2、BootReceiver.java

package cn.etzmico;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootReceiver extends BroadcastReceiver {
 public void onReceive(Context context, Intent intent) {
  if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
   Log.d("BootReceiver", "system boot completed");
   // context, AutoRun.class
   Intent newIntent = new Intent(context, AutoRun.class);
   /* MyActivity action defined in AndroidManifest.xml */
   newIntent.setAction("android.intent.action.MAIN");
   /* MyActivity category defined in AndroidManifest.xml */
   newIntent.addCategory("android.intent.category.LAUNCHER");
   /*
    * If activity is not launched in Activity environment, this flag is
    * mandatory to set
    */
   newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   //为刚要启动的Activity设置启动参数,此参数说明启动时为Activity开辟新的栈。
   /* if you want to start a service, follow below method */
   context.startActivity(newIntent);

//启动服务

/*

Intent intent = new Intent(IJetty.this,IJettyService.class);

                    intent.putExtra(__PORT,__PORT_DEFAULT);
                    intent.putExtra(__NIO,__NIO_DEFAULT);
                    intent.putExtra(__SSL,__SSL_DEFAULT);
                    intent.putExtra(__CONSOLE_PWD,__CONSOLE_PWD_DEFAULT);

                    //                    cw.startService(intent);

                    startService(intent);

 

xml文件中<service></service>

*/

 

 

  }

 }
}

3、AutoRun.java

package cn.etzmico;

import android.app.Activity;

import android.os.Bundle;
import cn.etzmico.autorun.R;

public class AutoRun extends Activity {

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  System.out.println("Successful");
 }
}

转载地址:http://cgfvl.baihongyu.com/

你可能感兴趣的文章
管理簇+创建簇索引+修改簇+删除簇
查看>>
New Concept English three(17)
查看>>
New Concept English three (53)
查看>>
CSS Hack
查看>>
Polysh实现多服务器批量执行shell
查看>>
矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
查看>>
jquery ajax中error返回错误解决办法
查看>>
maven核心,pom.xml详解
查看>>
Python2处理字符集问题
查看>>
互联网“平滑数据迁移”架构技术实践
查看>>
论相貌
查看>>
python爬虫和数据分析、数据挖掘
查看>>
我理解的数据库事务
查看>>
Tomcat的启动与关闭以及启动时一闪而过的解决方法
查看>>
composer设置忽略版本匹配
查看>>
D. Frets On Fire 前缀和+二分
查看>>
solrnet - document
查看>>
第十一节: 封装通用的定时调度框架,实现新增、删除、开启、暂停计划任务:...
查看>>
checkbox阻止事件
查看>>
关于HTTP协议学习(二)
查看>>