BOOT_COMPLETED从未收到 [英] BOOT_COMPLETED never received

查看:175
本文介绍了BOOT_COMPLETED从未收到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难道每一个设备发送BOOT_COMPLETED?我要开始启动的活动已完成。

我把下面的清单:

 <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_BOOT_COMPLETED/>
<接收机器人:BootFinishedNAME =>
    <意向滤光器>
         <作用机器人:名称=android.intent.action.BOOT_COMPLETED/>
    &所述; /意图滤光器>
< /接收器>
 

创建下面的类(接收器):

 进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.pm.PackageManager;
进口android.widget.Toast;

公共类BootFinished扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(上下文mContext,意图意图){
        如果(intent.getAction()。等于(android.intent.action.BOOT_COMPLETED)){
          //像做启动一个活动或服务
        }

        尝试 {
            PackageManager下午= mContext.getPackageManager();
            意向推出= pm.getLaunchIntentForPackage(com.example.afterboot);
            mContext.startActivity(发射);
        }赶上(例外五){
            Toast.makeText(mContext,e.getMessage(),Toast.LENGTH_SHORT);
        }
    }
}
 

我缺少的东西?谢谢!

解决方案
  1. 添加完整路径;其次在接收器添加许可。

     <接收器的Andr​​oid版本:NAME =com.example.BootFinished
       机器人:权限=android.permission.RECEIVE_BOOT_COMPLETED>
        <意向滤光器>
           <作用机器人:名称=android.intent.action.BOOT_COMPLETED/>
        &所述; /意图滤光器>
     < /接收器>
     

Does every device send the BOOT_COMPLETED? I want to start an Activity on Boot Completed.

I put the following in the Manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootFinished">            
    <intent-filter>                
         <action android:name="android.intent.action.BOOT_COMPLETED" />           
    </intent-filter>        
</receiver>

Created the following class (receiver):

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.Toast;

public class BootFinished extends BroadcastReceiver {

    @Override        
    public void onReceive(Context mContext, Intent intent) {
        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {               
          //do something like start an activity or service
        }

        try {
            PackageManager pm = mContext.getPackageManager();
            Intent launch = pm.getLaunchIntentForPackage("com.example.afterboot");
            mContext.startActivity(launch);
        } catch (Exception e) {
            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT);
        } 
    }
}

Am I missing something? Thanks!

解决方案

  1. Add full path and secondly add permission in your receiver.

     <receiver android:name="com.example.BootFinished"
       android:permission="android.permission.RECEIVE_BOOT_COMPLETED">            
        <intent-filter>                
           <action android:name="android.intent.action.BOOT_COMPLETED" />           
        </intent-filter>        
     </receiver>
    

这篇关于BOOT_COMPLETED从未收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆