android开机自启动

摘要:
原则:在收到系统启动广播后,启动一个透明活动,并在该活动中启动服务。关键代码如下:1。开机广播的接收器?123456789101112131415161718192021222324252627publicclassBootReceivereextendsBroadcastReceiver{publicvotionReceive{if{Log.d;//context,AutoRun.classIntentnewIntent=newIntent;/*MyActivityAction定义的AndroidManifest.xml*/newIntent.setAction;/*My Activity类别定义的AndridManifest.xml*/newIntint.addCategory;/**如果活动未在Activity环境中启动,则此标志*制造商设置*/newIntent。setFlags;/*如果要启动服务,请遵循以下方法*/context.startActivity;}}}2。启动后启动的活动是否透明?12345678910111213141516publicclassAutoRunextendsActivity{/**首次创建活动时调用。*/@OverridepublicvotionCreate{super.onCreate;//删除标题requestWindowFeature;//移除状态栏getWindow().setFlags;setContentView;Log.i;startService;finish();}}3。启动服务?

原理,在收到系统开机广播后,启动一个透明的activity,在activity里面启动一个服务。

关键代码如下:

1、开机广播接受者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
publicclassBootReceiver extendsBroadcastReceiver {
    publicvoidonReceive(Context context, Intent intent) {
 
        if(intent.getAction().equals(android.intent.action.BOOT_COMPLETED)) {
            Log.d(BootReceiver, system boot completed);
 
            // context, AutoRun.class
            Intent newIntent = newIntent(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);
 
            /* if you want to start a service, follow below method */
            context.startActivity(newIntent);
             
        }
    }
}



2、开机启动的activty透明

1
 
1
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
publicclassAutoRun extendsActivity {
    /** Called when the activity is first created. */
    @Override
    publicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         //去除title      
        requestWindowFeature(Window.FEATURE_NO_TITLE);      
        //去掉Activity上面的状态栏   
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        Log.i(开机启动,开机启动);
        startService(newIntent(this,EndClientService.class));
        finish();
         
    }
}



3、开机启动的服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
publicclassEndClientService extendsService {
    privateIntent intent2=null;
    publicEndClientService() {
        // TODO Auto-generated constructor stub
    }
 
    @Override
    publicvoidonCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.i(开机服务,服务开启);
        IntentFilter filter=newIntentFilter();
        filter.addAction(android.provider.Telephony.SMS_RECEIVED);
        filter.setPriority(Integer.MAX_VALUE);
        registerReceiver(mReceiver, filter);
         
         
    }
    @Override
    publicintonStartCommand(Intent intent, intflags, intstartId) {
        Log.i(服务,服务运行中);
        returnsuper.onStartCommand(intent, flags, startId);
    }
    @Override
    publicvoidonDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregisterReceiver(mReceiver);
        mReceiver=null;
    }
    @Override
    publicIBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        returnnull;
    }
1
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
1
2
3
4
5
6
7
<receiver android:label="@string/app_name"android:name="com.yqq.endClient3.BootReceiver">
            <intent-filter android:priority="1000">
                 
 
                <category android:name="android.intent.category.LAUNCHER">
            </category></action></intent-filter>
        </receiver>
 

推推族,免费得门票,游景区:www.tuituizu.com

结伴旅游,一个免费的交友网站:www.jieberu.com

免责声明:文章转载自《android开机自启动》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇CSMA-CA介绍SQL Server索引语法 &amp;lt;第四篇&amp;gt;下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android 设置Activity样式 透明度

一、设置Activity透明度有几种方法:1>.在清单文件中配置Activity时声明android:theme="@android:style/Theme.Translucent"2>.使用自定义主题,先看看自定义主题中需要用到的一些属性设置说明 <style name="custom"parent="@android:style/Th...

深入研究 .NET 5 的开放式遥测

OpenTelemetry 介绍 OpenTelemetry是一种开放的源代码规范,工具和SDK,用于检测,生成,收集和导出遥测数据(指标,日志和跟踪),开放遥测技术得到了Cloud Native Computing Foundation(CNCF)的支持,该基金会支持一系列流行的优秀的开源项目,你可以去看一下CNCF景观图,https://landsca...

Android开发高级进阶——多进程间通信

一. 什么是多进程? 多进程就是多个进程的意思,那么什么是进程呢? 当一个应用在开始运行时,系统会为它创建一个进程,一个应用默认只有一个进程,这个进程(主进程)的名称就是应用的包名。 进程的特点: 进程是系统资源和分配的基本单位,而线程是调度的基本单位。 每个进程都有自己独立的资源和内存空间 其它进程不能任意访问当前进程的内存和资源 系统给每个进程分...

android中的资源访问

一.android中的资源是在代码中使用的外部文件。图片,音频,动画和字符串等叫做android中的资源文件。 二.Android工程 资源类型布局表    与src源文件夹并列的两个文件夹assets和res用来保存资源文件。      1.assets文件夹中放原声文件如MP3文件,通过AssetManager类以二进制流的形式访问      2.re...

【腾讯Bugly干货分享】Android 插件技术实战总结

本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/1p5Y0f5XdVXN2EZYT0AM_A 前言 安卓应用开发的大量难题,其实最后都需要插件技术去解决。 现今插件技术的使用非常普遍,比如微信、QQ、淘宝、天猫、空间、携程、大众点评、手机管家等等这些大家在熟悉...

Activity切换动画---点击哪里从哪放大

本篇文章已授权微信公众号 安卓巴士Android开发者门户 独家发布 emmmm,这次来梳理一下 Activity 切换动画的研究。首先,老规矩,看一下效果图: 效果图 这次要实现的动画效果就是类似于上图那样,点击某个 view,就从那个 view 展开下个 Activity,Activity 退出时原路返回,即缩放到点击的那个 view。 实现思路 e...