zoukankan      html  css  js  c++  java
  • android如何实现开机自动启动Service或app

    第一步:首先创建一个广播接收者,重构其抽象方法 onReceive(Context context, Intent intent),在其中启动你想要启动的Service或app。

       import android.content.BroadcastReceiver;  
       import android.content.Context;  
       import android.content.Intent;  
       import android.util.Log;  
         
       public class BootBroadcastReceiver extends BroadcastReceiver {  
           //重写onReceive方法  
           @Override  
           public void onReceive(Context context, Intent intent) {  
               //后边的XXX.class就是要启动的服务  
               Intent service = new Intent(context,XXXclass);  
               context.startService(service);  
               Log.v("TAG", "开机自动服务自动启动.....");  
              //启动应用,参数为需要自动启动的应用的包名 
       Intent intent = getPackageManager().getLaunchIntentForPackage(packageName); 
       context.startActivity(intent );        
           }  
         
       } 

    第二步:配置xml文件,在receiver接收这种添加intent-filter配置  
         <receiver android:name="BootBroadcastReceiver">  
                    <intent-filter>  
                        <action android:name="android.intent.action.BOOT_COMPLETED"></action>  
                        <category android:name="android.intent.category.LAUNCHER" />  
                    </intent-filter>  
                </receiver>  
    第三步:添加权限 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

  • 相关阅读:
    如何下载网络图片资源
    经典排序之快速排序(含红黑树)
    经典排序之归并排序
    node微信公众号开发---自动回复
    koa2的文件上传
    async await的用法
    Generator yield语法和 co模块
    CentOS 7 下安装 Nginx
    windows下nginx的安装及使用方法入门
    linux下nodejs安装以及如何更新到最新的版本
  • 原文地址:https://www.cnblogs.com/exmyth/p/5501297.html
Copyright © 2011-2022 走看看