【问题标题】:broadcastreceiver does not kick in after rebooting重启后广播接收器不启动
【发布时间】:2016-10-12 09:56:17
【问题描述】:

早安,

我正在基于此创建一个应用程序。

BroadcastReceiver and alarmManager Android

运行顺利,
然后我尝试通过添加它来让它运行,即使客户端启动它的手机。

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

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

问题是,重启后广播接收器丢失。

我的BroadcastReceiver看起来像这样

public class MyBroadcastReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        Toast.makeText(context, "Don't panik but your time is up!!!!.",
                Toast.LENGTH_LONG).show();
        // Vibrate the mobile phone
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(2000);
    }
}

}

这已经解决了。谢谢。 (供将来参考):)

【问题讨论】:

  • 在您的BroadcastReceiver 中,您是否听过android.intent.action.BOOT_COMPLETED 的行动?请添加您的接收器类代码。
  • 嗨,我发布了我的广播接收器,你介意看看可能缺少的部分吗?谢谢!
  • 请看我的回答。

标签: android alarmmanager android-pendingintent android-broadcastreceiver


【解决方案1】:

尝试更新到以下内容。

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            //Do your stuffs here
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多