• 使用BroadcastReceiver实现开机启动Service或Activity


    引用:http://blog.csdn.net/gumanren/article/details/6218925

    比起symbian和j2me,android还是很容易实现开机自动启动应用的: 
     
     
    首先必须有一个BroadcastReceiver以便监听手机 开机intent, 
    而该receiver又负责启动你的service或者activity. 
     
     
    public class yourReceiver extends BroadcastReceiver { 
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent i = new Intent(yourService.ACTION_START);
            i.setClass(context, yourService.class);
            context.startService(i);
        }
    }
     
     
     
    其次,就需要在Manifest文件中声明一下intent-filter: 
    先加入使用权限声明:
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 
    然后加入receiver 注册声明: 
    <receiver android:name=".yourReceiver" >
        <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
       </intent-filter>
      </receiver>
     
     
    这样开机后yourReceiver 就能收到开机的intent,然后自动启动你的service 或activity.
    步骤:
    1、需要BroadcastReceiver

    2、使用

    <receiver android:name=".yourReceiver" >
        <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
       </intent-filter>
      </receiver>
     

     当手机系统启动完成后就启动此Receiver

    3.权限设置 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

    功能:当手机系统启动完成后,直接启动某个程序或者Activity,这里直接启动某个程序

  • 相关阅读:
    Spark:The Definitive Book第十四章笔记
    Spark:The Definitive Book第十三章笔记
    Spark:The Definitive Book第十二章笔记
    Spark:The Definitive Book第十一章笔记
    Spark:The Definitive Book第十章笔记
    Spark:The Definitive Book第九章笔记
    Spark:The Definitive Book第八章笔记
    Spark:The Definitive Book第七章笔记
    QRTest
    flutter笔记
  • 原文地址:https://www.cnblogs.com/sode/p/2406964.html
Copyright © 2020-2023  润新知