3

I want to repeat an action every day; it must continue working even if the app is not running or the device has been restarted (rebooted). In my code I'm trying to show a TOAST message every 1 minute (as a test); it's working fine in the emulator but on a real device it doesn't work( i tried to do some changes to fixed as i see in some answers but still the same thing)

MyReceiver

class MyReceiver : BroadcastReceiver() {
    private val channelId = "com.medanis.hikamwahimam"

    override fun onReceive(context: Context, intent: Intent) {
        Log.i("TAG","/////////////////// SHOW NOTIFICATION NOW //////////////////////")
        val builder = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setLargeIcon(BitmapFactory.decodeResource(context.resources,R.mipmap.ic_launcher_round))
            .setContentTitle("My notification")
            .setContentText("Much longer text that cannot fit one line...")
            .setStyle(
                NotificationCompat.BigTextStyle()
                    .bigText("Much longer text that cannot fit one line...Much longer text that cannot fit one line..."))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        with(NotificationManagerCompat.from(context)) {
            notify(12345, builder.build()) }
        Toast.makeText(context,"This toast will be shown every X minutes", Toast.LENGTH_LONG).show()
    }
}

MainActivity

class MainActivity : AppCompatActivity() {
    private var mAlarmManager : AlarmManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
//        showNotification()

        val mIntent = Intent(this, MyReceiver::class.java)

        val mPendingIntent = PendingIntent.getBroadcast(this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT)
        mAlarmManager = this
            .getSystemService(Context.ALARM_SERVICE) as AlarmManager
        mAlarmManager!!.setRepeating(
            AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
            60000, mPendingIntent
        )
    }
}

AndroidManifest.xml

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

The Issues are :

1/- This code does not work with REAL DEVICE.

2/- This code does not work if the user restart his device.

A sample on GitHub (i had made some changes as my friend had suggested but still the same errors)

Zoe
  • 27,060
  • 21
  • 118
  • 148
WIKOSO
  • 105
  • 1
  • 2
  • 12
  • You should consider using the WorkManager library as in [this question](https://stackoverflow.com/q/50357066/2280156). You can learn more at [the tutorial](https://developer.android.com/topic/libraries/architecture/workmanager/how-to/recurring-work) on the official site. – Mark S Mar 28 '19 at 17:31
  • can you give a sample to the idea in github please ! – WIKOSO Mar 28 '19 at 18:20

1 Answers1

1

1/- This code does not work with REAL DEVICE.

I've downloaded your project, run on my device and it works, it shows the Toast when I click start, and every minute is showing.

I recommend you to take a look on this question

2/- This code does not work if the user restart his device.

If you want to restart your BroadcastReceiver once the device is rebooted or shut downed, you may want to add this code :

Add this in your manifest.xml

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

Create another receiver in manifest.xml

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

And then create a BroadcastReceiver as you did previously for showing the Toast

class BootCompletedIntentReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if ("android.intent.action.BOOT_COMPLETED" == intent.action) {
          //Start your old broadcastreceiver
        }
    }
}

For more information you can take a look at this post

Hope it helps.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • can you please add your modification to my code and push it to the github project – WIKOSO Mar 28 '19 at 14:26
  • i made some changes in the code as i understand but still the same issues please check it again ( my device API is 24 ) – WIKOSO Mar 28 '19 at 16:34
  • sometimes device block boot broadcast. Go to settings -> battery -> your app and watch if there is toogle to enable receive boot – Nikolay Gonza Mar 29 '19 at 00:25
  • Do what Nikolay said @WIKOSO – Skizo-ozᴉʞS ツ Mar 29 '19 at 08:54
  • i have the same problem @Skizo-ozᴉʞS i tried all things above but nothing has changed ( in the first time i was trying the app on my phone " Condor M1 " now i had tried with " Samsung " and still the same thing) please go back to my code on github, check it and try it with your device – WIKOSO Mar 29 '19 at 10:14
  • HEEELp me please – WIKOSO Apr 01 '19 at 17:23