Broadcast Receiver wont start Activity when app is not running

0

I am trying to open activity through Broadcast Receiver.

Here is the Manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.andreasgift.myclock">

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@drawable/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".Alarm.AlarmNotifActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SET_ALARM" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <receiver android:name=".Alarm.AlarmReceiver" />
    </application>

</manifest>

Here is the activity that I am trying to start

class AlarmNotifActivity : AppCompatActivity() {
    private val snoozeTiming = 600000L

    private var label: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("ALARM","alarmnotif activity")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_alarm_notif)

        intent.getStringExtra(Constants().ALARM_LABEL_KEY)?.let {
            label = it
            this.label_tv.setText(label)
        }
    }

    override fun onResume() {
        super.onResume()
        Log.d("ALARM","RESUME ACR")
    }

    override fun onStart() {
        super.onStart()
        Log.d("ALARM","ON START")
    }

    fun dismissButton(view: View) {
        finish()
    }

    fun snoozeButton(view: View) {
        val alarmManager = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val nextintent = Intent(this, AlarmReceiver::class.java)
        label?.let { intent.putExtra(Constants().ALARM_LABEL_KEY, label) }
        val pendingIntent = PendingIntent.getBroadcast(
            this@AlarmNotifActivity,
            0,
            nextintent,
            PendingIntent.FLAG_UPDATE_CURRENT
        )
        alarmManager.set(
            AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + snoozeTiming,
            pendingIntent
        )
        finish()
    }
}

And here is the Broadcast Receiver class:

class AlarmReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val openNext = Intent(context, AlarmNotifActivity::class.java)
        openNext.putExtra(Constants().ALARM_LABEL_KEY, intent.getStringExtra(Constants().ALARM_LABEL_KEY))
        openNext.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(openNext)
        Log.d("ALARM","Broadcast receiver working")
    }
}

The feature is working fine when the application is open. However it seems wont execute the startActivity(Intent) when the application is closed. But it did execute the last log.d in the broadcast receiver.

I have tried to replace context with context.applicationContext, but it didn't change anything. I have tried to replace the activity class to another activity, but also no succeed. I have tried to replace the action from open activity into showing toast. Toast is shown, work perfectly fine.

Here is the logcat after the Broadcast Receiver execute the onReceive

2019-11-05 16:42:29.962 1992-3189/system_process I/ActivityTaskManager: START u0 {flg=0x10000000 cmp=com.andreasgift.myclock/.Alarm.AlarmNotifActivity (has extras)} from uid 10133
2019-11-05 16:42:29.962 1992-3189/system_process W/ActivityTaskManager: Background activity start [callingPackage: com.andreasgift.myclock; callingUid: 10133; isCallingUidForeground: false; isCallingUidPersistentSystemProcess: false; realCallingUid: 10133; isRealCallingUidForeground: false; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: null; isBgStartWhitelisted: false; intent: Intent { flg=0x10000000 cmp=com.andreasgift.myclock/.Alarm.AlarmNotifActivity (has extras) }; callerApp: ProcessRecord{c678bfd 7783:com.andreasgift.myclock/u0a133}]
2019-11-05 16:42:29.963 1992-2036/system_process I/libprocessgroup: Successfully killed process cgroup uid 10095 pid 6926 in 79ms
2019-11-05 16:42:29.964 7783-7783/com.andreasgift.myclock D/ALARM: Broadcast receiver working

Anybody have idea what goes wrong here?

kotlin
android-intent
android-activity
broadcastreceiver
asked on Stack Overflow Oct 31, 2019 by Andrea Liu • edited Nov 5, 2019 by Andrea Liu

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0