<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mahmoud Mabrok Fouad</title>
    <description>The latest articles on DEV Community by Mahmoud Mabrok Fouad (@mahmoudmabrok).</description>
    <link>https://dev.to/mahmoudmabrok</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F253112%2Fa7d52584-1d18-4966-8474-aa75932333ea.jpg</url>
      <title>DEV Community: Mahmoud Mabrok Fouad</title>
      <link>https://dev.to/mahmoudmabrok</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mahmoudmabrok"/>
    <language>en</language>
    <item>
      <title>TIL 1: PendingIntent &amp; WorkManager</title>
      <dc:creator>Mahmoud Mabrok Fouad</dc:creator>
      <pubDate>Mon, 17 Jan 2022 19:19:42 +0000</pubDate>
      <link>https://dev.to/mahmoudmabrok/til-1-pendingintent-workmanager-4196</link>
      <guid>https://dev.to/mahmoudmabrok/til-1-pendingintent-workmanager-4196</guid>
      <description>&lt;p&gt;Today I learned &lt;strong&gt;a.k.a&lt;/strong&gt; TIL a &lt;strong&gt;series&lt;/strong&gt; of posts where I share staff i learned daily. &lt;/p&gt;

&lt;h2&gt;
  
  
  PendingIntent
&lt;/h2&gt;

&lt;p&gt;I was developing a Notification-App and used PendingIntent with Notification and faced a case that seems strange. &lt;/p&gt;

&lt;p&gt;The case is that PendingIntent open Activity with with extra string but when two Notifications comes with two different strings when clicked on both i got first string of them in two cases while i expect getting two different strings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;        &lt;span class="c1"&gt;// Create an explicit intent for an Activity in your app&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;intent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;MainActivity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_ACTIVITY_NEW_TASK&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="nc"&gt;Intent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FLAG_ACTIVITY_CLEAR_TASK&lt;/span&gt;
            &lt;span class="nf"&gt;putExtra&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AppConstants&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NOTIFICATION_EXTRA_TITLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;putExtra&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AppConstants&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NOTIFICATION_EXTRA_MESSAGE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;


        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;pendingIntent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PendingIntent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
            &lt;span class="nc"&gt;PendingIntent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;intent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The issue
&lt;/h3&gt;

&lt;p&gt;was that two PendingINtent have same requestCode (second params of &lt;code&gt;getActivity()&lt;/code&gt;) &lt;/p&gt;

&lt;h3&gt;
  
  
  The solution
&lt;/h3&gt;

&lt;p&gt;is two have unique/disticnt request code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  val pendingIntent: PendingIntent =
            PendingIntent.getActivity(context, Random.nextInt(), intent, 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Random.nextInt()&lt;/code&gt; made the trick for me. &lt;/p&gt;




&lt;h2&gt;
  
  
  WorkManager
&lt;/h2&gt;

&lt;p&gt;To schedule the notification I have used WorkManager but want to make the request be unique so as not to be scheduled each time user open the app. &lt;/p&gt;

&lt;h3&gt;
  
  
  What I used:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;  &lt;span class="n"&gt;workManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueueUniquePeriodicWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;AppConstants&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;QUEOTES_NOTIFICATION_WORKER_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;ExistingPeriodicWorkPolicy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;KEEP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;request&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;AppConstants.QUEOTES_NOTIFICATION_WORKER_ID&lt;/code&gt; is uniqueWorkName&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ExistingPeriodicWorkPolicy.KEEP&lt;/code&gt; If there is existing pending (uncompleted) work with the same unique name, do nothing. Otherwise, insert the newly-specified work.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Fire Worker Immediately:
&lt;/h3&gt;

&lt;p&gt;I want to make my periodic worker start immediately i.e run for the first time without waiting for the first iteration of the scheduling &lt;/p&gt;

&lt;h4&gt;
  
  
  Solution
&lt;/h4&gt;

&lt;p&gt;add next to worker request &lt;code&gt;.setInitialDelay(-1, TimeUnit.MINUTES)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  make  Periodic tasks start after some time:
&lt;/h3&gt;

&lt;p&gt;We may need our periodic worker to start from future time instead of now, with latest WorkManager this is allowed for both   OneTimeWorkRequest and PeriodicWorkRequest with help of &lt;code&gt;.setInitialDelay(10, TimeUnit.MINUTES)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Hope this adds new knowledge for you, meet next time. &lt;/p&gt;

</description>
      <category>android</category>
    </item>
    <item>
      <title>A gradle Task to Save your time</title>
      <dc:creator>Mahmoud Mabrok Fouad</dc:creator>
      <pubDate>Sat, 25 Dec 2021 18:07:39 +0000</pubDate>
      <link>https://dev.to/mahmoudmabrok/a-gradle-task-to-save-your-time-2nc5</link>
      <guid>https://dev.to/mahmoudmabrok/a-gradle-task-to-save-your-time-2nc5</guid>
      <description>&lt;p&gt;Recently, while running Android Studio I got next error from time to time which waste much time which can be increased based on project size (at least 1 minute, so this amount is &lt;strong&gt;multiplied&lt;/strong&gt; with each build we do): &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The process cannot access the file because it is being used by another process.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This can be &lt;strong&gt;caused&lt;/strong&gt; when some &lt;strong&gt;plugins&lt;/strong&gt; run &lt;em&gt;another Java instance&lt;/em&gt; so they &lt;strong&gt;lock&lt;/strong&gt; files from one to the other &lt;strong&gt;So&lt;/strong&gt; solution is to kill java process by using: &lt;br&gt;
&lt;code&gt;TASKKILL /F /IM java.exe&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;So I searched to make this automated somehow And found we can declare simple gradle task to do this job, it as next( write it at &lt;code&gt;build.gradle&lt;/code&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="nf"&gt;killJavaProcess&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;type:&lt;/span&gt;&lt;span class="n"&gt;Exec&lt;/span&gt; &lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="n"&gt;commandLine&lt;/span&gt; &lt;span class="s1"&gt;'cmd'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'/c'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'TASKKILL /F /IM java.exe'&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then to make it be done before each build go to build task at right side of gradle and choose to run before build as next: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--p1asPvkC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ky2v78kfagpnvccqdn52.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--p1asPvkC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ky2v78kfagpnvccqdn52.png" alt="image with steps " width="747" height="625"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have nice coding. &lt;/p&gt;

&lt;h2&gt;
  
  
  Resources:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html"&gt;gradle Exec Task type&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>gradle</category>
    </item>
  </channel>
</rss>
