引导接收器从未在引导后被调用但是接收处理程序是,并且有数据?

时间:2011-02-01 14:19:42

标签: android broadcastreceiver boot

我已经读过,当您使用Android重启时,所有的AlarmManager都被删除,需要重新制作。所以我创建了一个BootReciever类,通过调用WakefulIntentService来重启我的所有警报: -

public class BootReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.v("MessageDelay", "Reboot message Recieved");

        try {

            WakefulIntentService.sendWakefulWork(context, BootSetter.class);    


            } catch (Exception e) {
             Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
             e.printStackTrace();

            }

    }
 }

在Manifest中,我声明并设置了一个intent过滤器,因此它只在启动完成后才会运行: -

  <receiver android:name=".BootReciever">
         <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED" />
         </intent-filter>
        </receiver>

现在,在我的头发试图修复一个不能正常工作的问题后,我已经意识到我的BootReciever类在启动后没有被调用。相反,我的另一个名为RecieverHandler的类正在被召唤。更令人费解的是,在启动时传递给我的RecieverHandler的Intent实际上拥有可以无误使用的有效数据。

My RecieverHandler看起来像这样: -

public class RecieverHandler extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {

        try {

             Bundle bundle = intent.getExtras();

             Log.v("MessageDelay", "RecieverHandler is doing boot jobs");

             Intent newIntent = new Intent(context, MessageDispatcher.class);
             newIntent.putExtras(bundle);
            // newIntent.addFlags(Intent.FLAG);
             //context.startService(newIntent);
             WakefulIntentService.sendWakefulWork(context, newIntent);    


            } catch (Exception e) {
             Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
             e.printStackTrace();

            }


    }
}

它在清单中声明如下:

<receiver android:name=".RecieverHandler">
        </receiver>

虽然在另一种情况下可能没问题但我需要在手机开机和手机启动时使用不同的接收器。

总结疑问是: -

为什么RecieverHandler处理Boot完成的Intent和我的BootReciever永远不会被调用?

重启后,重启前的Intent数据集如何仍然有效?

在设备的短信发送功能准备就绪之前,他们是否可以延迟任何操作?

1 个答案:

答案 0 :(得分:2)

您还需要设置接收此意图的权限。  &LT; uses-permission android:name =“android.permission.RECEIVE_BOOT_COMPLETED”/&gt; 有关详细信息,请访问:http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/