PendingIntent.getBroadcast()永远不会返回null [英] PendingIntent.getBroadcast() never returns null

查看:470
本文介绍了PendingIntent.getBroadcast()永远不会返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到的PendingIntent 我的广播接收器这是使用称为 AlarmManager

我使用 PendingIntent.getBroadcast()使用它被称为具有非常相同的参数,但它永远不会返回null。

 公开为MyObject(上下文的背景下,AnotherObject对象){    我的意图;
    I =新意图(背景下,MyReceiver.class);
    i.putExtra(AnotherObject.SOMETHING,串);
    i.putExtra(AnotherObject.SOME_BOOL,真);    PI的PendingIntent = PendingIntent.getBroadcast(背景下,0,I,PendingIntent.FLAG_NO_CREATE);    如果(PI == NULL){
        Log.v(帮助,的PendingIntent为空);
    }
    其他{
        Log.v(帮助,的PendingIntent不为空);
    }
}

下面是我的测试驱动程序:

  =该新MyObject来(这一点,对象);LogUtil.startAlarm(这一点,对象);该=新LogUtil(这一点,对象);

这是 startAlarm

 公共静态无效startAlarm(背景信息,AnotherObject对象){    我的意图;
    I =新意图(即,MyReceiver.class);
    i.putExtra(AnotherObject.SOMETHING,串);
    i.putExtra(AnotherObject.SOME_BOOL,真);    长间隔= 60 * 1000; //分钟
    长的第一= SystemClock.elapsedRealtime()+区间;    PI的PendingIntent = PendingIntent.getBroadcast(即,0,I,PendingIntent.FLAG_UPDATE_CURRENT);    //安排报警
    AlarmManager AM2 =(AlarmManager)that.getSystemService(Context.ALARM_SERVICE);
    am2.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,第一次,间隔,PI);
    Log.v(帮助,启动报警);
}

下面是我的日志输出:

 十一月9日至2日:18:54.330:VERBOSE /帮助(9621)的PendingIntent不为空
十一月九日至2日:18:54.330:VERBOSE /帮助(9621):开始报警
十一月九日至2日:18:54.330:VERBOSE /帮助(9621)的PendingIntent不为空

为什么它说这是从来没有空,甚至在我启动报警?不应该 getBroadcast()返回NULL?


解决方案

  

我试图找到我的BroadcastReceiver的的PendingIntent被使用AlarmManager调用。


呃,为什么?


  

我使用PendingIntent.getBroadcast()使用,它调用了同样的参数,但它永远不会返回null。


这不应该。这将创建一个的PendingIntent 如果没有一个等效的底层意图。有人可能会说,该方法的名称应该是 createBroadcast()来使这一点更加清晰。

I am trying to find the PendingIntent of my BroadcastReceiver that was called using an AlarmManager.

I am using PendingIntent.getBroadcast() using the very same arguments that it was called with, however it never returns null.

public MyObject(Context context, AnotherObject object) {

    Intent i;
    i = new Intent(context, MyReceiver.class);
    i.putExtra(AnotherObject.SOMETHING, "string");
    i.putExtra(AnotherObject.SOME_BOOL, true);

    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_NO_CREATE);

    if (pi == null) {
        Log.v("help", "PendingIntent is null");
    }
    else {
        Log.v("help", "PendingIntent is not null");
    }
}

Here is my test driver:

that = new MyObject(this, object);

LogUtil.startAlarm(this, object);

that = new LogUtil(this, object);

This is startAlarm:

public static void startAlarm(Context that, AnotherObject object) {

    Intent i;
    i = new Intent(that, MyReceiver.class);
    i.putExtra(AnotherObject.SOMETHING, "string");
    i.putExtra(AnotherObject.SOME_BOOL, true);

    long interval = 60 * 1000; // Minute
    long first = SystemClock.elapsedRealtime() + interval;

    PendingIntent pi = PendingIntent.getBroadcast(that, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

    // Schedule the alarm
    AlarmManager am2 = (AlarmManager) that.getSystemService(Context.ALARM_SERVICE);
    am2.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, first, interval, pi);
    Log.v("help", "Started alarm");
}

Here is my log output:

09-02 11:18:54.330: VERBOSE/help(9621): PendingIntent is not null
09-02 11:18:54.330: VERBOSE/help(9621): Started alarm
09-02 11:18:54.330: VERBOSE/help(9621): PendingIntent is not null

Why does it say it's never null, even before I start the alarm? Shouldn't getBroadcast() return null?

解决方案

I am trying to find the PendingIntent of my BroadcastReceiver that was called using an AlarmManager.

Um, why?

I am using PendingIntent.getBroadcast() using the very same arguments that it was called with, however it never returns null.

It's not supposed to. It will create a PendingIntent if there is not already one for an equivalent underlying Intent. One could argue that the method name ought to be createBroadcast() to make that point clearer.

这篇关于PendingIntent.getBroadcast()永远不会返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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