安卓:广播接收机将不会收到开机消息 [英] Android: Broadcast receiver won't receive boot up message

查看:153
本文介绍了安卓:广播接收机将不会收到开机消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 MyBroadcastReceiver 被注册在启动时启动。清单如下。我通过对Android的摩托罗拉Xoom wifi的平板电脑专用日食运行此 - 这是Android 3.2的,API等级13(从dev.android网站)

我用Google搜索这个,并试图几件事情:


  • 的android:出口=真正的


  • 使用安卓的installLocation =internalOnly,以确保它不会在SD卡上安装


  • 在设备上安装后,我跑了应用几次测试启动(如一些链接声称Android 3.X起将不会立即开始发送引导事件到应用程序,除非用户手动启动之前它至少一次)


然而,即使现在的广播接收器似乎并不在设备启动(基于logcat中)运行。

下面是我的清单文件:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=com.mypack
      安卓版code =1
      机器人:=的versionName1.0
      安卓的installLocation =internalOnly>
    <使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>
    <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>    <采用-SDK安卓的minSdkVersion =8/>    <应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
      <活动机器人:MainCamAppNAME =机器人:标签=@字符串/ APP_NAME>
      &所述;意图滤光器>
          <作用机器人:名字=android.intent.action.MAIN/>
          <类机器人:名字=android.intent.category.LAUNCHER/>
      &所述; /意图滤光器>
      < /活性GT;     <服务机器人:名字=。UploaderService/>     <接收机器人:MyBroadcastReceiverNAME =机器人:启用=真正的机器人:出口=真正的>
      &所述;意图滤光器>
          <作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
      &所述; /意图滤光器>
     < /接收器>    < /用途>
< /清单>

code广播接收器 -

 进口java.util.Calendar中;进口android.app.AlarmManager;
进口android.app.PendingIntent;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.util.Log;公共类MyBroadcastReceiverX扩展广播接收器{
    私人字符串标记=广播​​接收机:
    AlarmManager报警;    @覆盖
    公共无效的onReceive(上下文为arg0,ARG1意向){
        Log.i(TAG,输入的onReceive。);
        报警=(AlarmManager)arg0.getSystemService(Context.ALARM_SERVICE);
        意图int​​entUploadService =新意图(arg0中,UploaderService.class);        台历挂历= Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis的());
        calendar.add(Calendar.SECOND,10);        的PendingIntent圆周率= PendingIntent.getBroadcast(为arg0,0,intentUploadService,0);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),5000,PI);
        Log.i(TAG,报警设定退出的onReceive。);        意图myIntent =新意图(arg0中,UploaderService.class);
        arg0.startService(myIntent);
    }}


似乎是现在的工作。下面,如果它有助于最终清单 -

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=com。示例
      安卓版code =1
      机器人:=的versionName1.0
      安卓的installLocation =internalOnly
      机器人:权限=android.permission.RECEIVE_BOOT_COMPLETED>    <使用许可权的android:NAME =android.permission.CAMERA/>
    <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
    <使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>    <采用-SDK安卓的minSdkVersion =8/>
    <使用特征的android:NAME =android.hardware.camera/>
    <使用特征的android:NAME =android.hardware.camera.autofocus/>    <应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
      <活动机器人:MainCamAppNAME =机器人:标签=@字符串/ APP_NAME>
      &所述;意图滤光器>
          <作用机器人:名字=android.intent.action.MAIN/>
          <类机器人:名字=android.intent.category.LAUNCHER/>
      &所述; /意图滤光器>
      < /活性GT;     <服务机器人:名字=。UploaderService/>    <接收机器人:MyBroadcastReceiverXNAME =机器人:启用=真正的机器人:权限=android.permission.RECEIVE_BOOT_COMPLETED>
      &所述;意图滤光器>
          <作用机器人:名字=android.intent.action.BOOT_COMPLETED>
          <类机器人:名字=android.intent.category.DEFAULT/>
          < /作用>
      &所述; /意图滤光器>
    < /接收器>    < /用途>
< /清单>


解决方案

请尝试以下操作:

 <接收机器人:MyBroadcastReceiverXNAME =机器人:启用=真正的机器人:权限=android.permission.RECEIVE_BOOT_COMPLETED>
  &所述;意图滤光器>
      <作用机器人:名字=android.intent.action.BOOT_COMPLETED>
      <类机器人:名字=android.intent.category.DEFAULT/>
      < /作用>
  &所述; /意图滤光器>
< /接收器>

您也可以在清单中指定不正确的类的名字 - 它应该是 MyBroadcastReceiverX 而不是 MyBroadcastReceiver

I've a class MyBroadcastReceiver that is registered to start at boot up. The manifest is below. I am running this via eclipse on a Android Motorola Xoom wifi only tablet - which is Android 3.2, API level 13 (from the dev.android site).

I have googled for this and tried a few things:

  • put android:exported="true"

  • used android:installLocation="internalOnly" to make sure it doesn't install on the SD card

  • after installing on the device, I ran the app a couple of times before testing the bootup (as some links said Android 3.x onwards won't immediately start sending boot events to the app unless user has manually initiated it at least once)

However even now the broadcast receiver doesn't seem to run when the device boots up (based on logcat).

Here is my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mypack"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="internalOnly">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name=".MainCamApp" android:label="@string/app_name">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      </activity>

     <service android:name=".UploaderService" />

     <receiver android:name=".MyBroadcastReceiver"  android:enabled="true" android:exported="true">
      <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED"/>
      </intent-filter>
     </receiver>

    </application>
</manifest>

Code for broadcast receiver -

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyBroadcastReceiverX extends BroadcastReceiver {
    private String TAG = "Broadcast Receiver:";
    AlarmManager alarm;

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Log.i(TAG, "Entered onReceive.");
        alarm = (AlarmManager) arg0.getSystemService(Context.ALARM_SERVICE);
        Intent intentUploadService = new Intent (arg0, UploaderService.class);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 10);

        PendingIntent pi = PendingIntent.getBroadcast(arg0, 0, intentUploadService , 0);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pi);
        Log.i(TAG, "Alarm set. Exiting onReceive.");

        Intent myIntent = new Intent(arg0, UploaderService.class);
        arg0.startService(myIntent);
    }

}


Seems to be working now.. final manifest below if it helps -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="internalOnly"
      android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <uses-sdk android:minSdkVersion="8" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name=".MainCamApp" android:label="@string/app_name">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      </activity>

     <service android:name=".UploaderService" />

    <receiver android:name=".MyBroadcastReceiverX"  android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
      <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED">
          <category android:name="android.intent.category.DEFAULT"/> 
          </action>
      </intent-filter>
    </receiver>

    </application>
</manifest>

解决方案

Try the following:

<receiver android:name=".MyBroadcastReceiverX"  android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
  <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED">
      <category android:name="android.intent.category.DEFAULT"/> 
      </action>
  </intent-filter>
</receiver>

You also have incorrect class name specified in manifest - it should be MyBroadcastReceiverX rather then MyBroadcastReceiver

这篇关于安卓:广播接收机将不会收到开机消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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