【问题标题】:Start BroadcastReceiver on Boot在启动时启动 BroadcastReceiver
【发布时间】:2015-11-26 21:42:21
【问题描述】:

当用户plug or unplug 其手机时,我正在尝试run code。 我想我可以通过创建一个广播接收器并将 Manifest 中的触发器设置为 android.intent.action.BOOT_COMPLETED 来实现这一点。
然后从该 BOOT_COMPLETED RECEIVER 启动一个服务并启动调试消息。
我从服务中为android.intent.action.ACTION_POWER_CONNECTEDandroid.intent.action.ACTION_POWER_DISCONNECTED 启动了另一个广播接收器。
一切顺利,调试消息被广播,服务为ACTION_POWER广播接收器执行了启动过程,但接收器没有触发。 有人知道如何解决它吗?

代码:
清单:

<application

    ...


    <receiver
        android:name="de.mrglue.nfc.BootReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

    <service android:name="de.mrglue.nfc.BootService" >
    </service>


</application>


<receiver android:name=".PowerConnectionReceiver">
    <intent-filter>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
    </intent-filter>
</receiver>




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

引导接收器:

public class BootReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    //Can't toast here
    //Starting service
    context.startService(new Intent(context, BootService.class));
}

服务:

public class BootService extends Service{

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Starting second Receiver!",   Toast.LENGTH_LONG).show();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(new PowerConnectionReceiver(), filter);
        Toast.makeText(this, "done!", Toast.LENGTH_LONG).show();
        stopSelf();
        return START_NOT_STICKY;
    }
}

电池接收器:

public class PowerConnectionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
            status == BatteryManager.BATTERY_STATUS_FULL;

        Toast.makeText(context, "CHARGE" + isCharging, Toast.LENGTH_LONG).show();

    }
}

【问题讨论】:

    标签: android broadcastreceiver


    【解决方案1】:

    您的清单文件中缺少电话状态权限。
    包括以下读取手机状态的权限:

    使用权限 android:name="android.permission.READ_PHONE_STATE

    【讨论】:

    • READ_PHONE_STATE 与我的目标有什么关系?
    【解决方案2】:

    它只需要干净且完全重启。

    编辑 1 号
    我将尝试通过添加更多细节来使我的答案更加清晰:

    安装新软件后设备需要完全重启才能看到
    的任何效果 新的 Broadcastreceivers 系统必须在没有运行应用程序的情况下执行。我认为调用意图android.intent.action.BOOT_COMPLETED 不会对设备产生任何影响。

    【讨论】:

    • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post。 - From Review
    • @NorbertvanNobelen 我不明白为什么这不算作答案。这是解决问题的可能方法。我不想批评或要求作者澄清,因为我是作者,目前我的问题没有任何问题。但我愿意学习,所以我会努力改进我的答案。无论如何,感谢您的评论。
    • 一个简短的答案(就像它最初的样子)被用作评论。主要是因为您没有像现在在编辑中那样指出根本原因等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    相关资源
    最近更新 更多