【问题标题】:My BroadcastReceiver is not receiving the BOOT_COMPLETED intent after my N1 bootsN1 启动后,我的 BroadcastReceiver 没有收到 BOOT_COMPLETED 意图
【发布时间】:2011-04-25 12:29:05
【问题描述】:

我无法使用 BOOT_COMPLETED 意图调用我的 BroadcastReceiver onReceive 方法。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.jerrellmardis.umbrella"
      android:versionCode="4"
      android:versionName="1.0.3">
    <application android:icon="@drawable/icon" android:label="@string/app_name" 
            android:theme="@android:style/Theme.NoTitleBar">
        <activity android:name=".activities.Umbrella" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activities.Preferences" android:label="@string/app_name" android:screenOrientation="portrait" />
        <receiver android:name="com.jerrellmardis.umbrella.receiver.WeatherStartupReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <service android:name=".service.WeatherUpdateService">
            <intent-filter>
                <action android:name="com.jerrellmardis.umbrella.service.WeatherUpdateService" />
            </intent-filter>
        </service>
    </application>
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>

WeatherStartupReceiver.java

package com.jerrellmardis.umbrella.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Contacts.People;
import android.util.Log;

import com.jerrellmardis.umbrella.R;

public class WeatherStartupReceiver extends BroadcastReceiver {

       private NotificationManager mNotificationManager;
       private int SIMPLE_NOTFICATION_ID;

       @Override
       public void onReceive(Context context, Intent intent) {
                // Do something interesting here...
       }
}

【问题讨论】:

  • oracleicom,问题是怎么解决的。请告诉

标签: android service broadcastreceiver android-intent intentfilter


【解决方案1】:

所有接收BOOT_COMPLETED 广播的应用程序都必须安装在内部存储上,因为Android 会在外部存储安装到设备之前传送ACTION_BOOT_COMPLETED 广播。

为确保您的应用程序将安装在内存中,您只需要NOT 声明android:installLocation 清单属性。

另一个选项是在清单部分设置以下内容:android:installLocation="internalOnly"

你可以找到更多关于它的信息here

【讨论】:

  • 您的链接包含导致 404 的句点。删除尾随句点可以解决问题。编辑已提交。
【解决方案2】:

编辑:忘记一切,我找到了更好的解释。

您必须使用exported = true 和enabled = true 定义您的接收器

<receiver android:name="com.jerrellmardis.umbrella.receiver.WeatherStartupReceiver"
  android:enabled="true" 
  android:exported="true" 
>

我认为如果你改变这一行

<receiver android:name="com.jerrellmardis.umbrella.receiver.WeatherStartupReceiver">

为此

<receiver android:name=".WeatherStartupReceiver">

它将解决您的问题。

我在我的一个项目中尝试过,但没有启动。

【讨论】:

  • 我不知道您为什么遇到问题,但我当然不必为我的 BOOT_COMPLETED 接收器声明启用和导出来接收通知。
  • 我已经重新阅读了有关此的 android 文档,当然他们说启用和导出的属性都默认设置为“true”......我又错了:S。我在 oracleicom 的代码上没有看到任何其他奇怪的地方
  • 即使该类不在我的应用程序目录的根目录下,也要将接收者的名称更改为“.WeatherStartupReceiver”?当前目录结构为/receiver/WeatherStartupReceiver.java
  • 所以我尝试在 AndroidManifest 中更改接收器的名称,但没有奏效。为了确保我没有注意到正在调用该方法,我更改了接收器的 onReceive 方法以引发异常,但仍然没有。
  • 已解决!!!它不起作用,因为该应用程序正在安装在 SD 卡上,因此缺少 BOOT_COMPLETED 意图。感谢大家的帮助!
猜你喜欢
  • 2011-06-30
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多