广播onReceive方法不会被调用

时间:2018-05-07 13:53:37

标签: android kotlin broadcastreceiver android-pendingintent

根据评论,代码更新了更具体的信息。

的Manifest.xml

<receiver
        android:name="com.x.x.x.MyReceiver"
        android:enabled="true"
        android:exported="false">

        <intent-filter>
            <action android:name="com.x.x.x.NOTIFICATION_INTENT_ACTION.TEST" />
        </intent-filter>
</receiver>

MyReceiver.java

internal class MyReceiver: BroadcastReceiver() {

    companion object {
        const val NOT_ID = ".."
        const val NOT = ".."
        const val NOTIFICATION_INTENT_ACTION = "com.x.x.x.NOTIFICATION_INTENT_ACTION.TEST"

    }

    override fun onReceive(context: Context, intent: Intent) {
        // does not get called
    }
}

CreatingIntent.java

    Intent intent = new Intent(this, MyReceiver.class);
    mPendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    mAlarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    if (mAlarmManager != null) {
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60000L, mPendingIntent);
    }

问题:NotRec类内部的onReceive方法不会被调用。我试图将Manifest更改为<receiver android:name="FULL_PATH.receiver.NotRec" />,但它并没有改变任何东西。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

删除

android:exported="false"

来自清单中的<receiver>声明。

如果你没有&#34;出口&#34;你的接收者,AlarmManager无法触发它。当您将清单中的某个组件标记为&#34;未导出&#34;时,这意味着它是&#34;私有&#34;和其他应用程序(包括AlarmManager等系统应用程序)无法启动或触发组件。