<?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: shubhcode</title>
    <description>The latest articles on DEV Community by shubhcode (@shubhcode).</description>
    <link>https://dev.to/shubhcode</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%2F669233%2F6cc3bbfc-3f5f-41b1-902f-7b613c0658b2.png</url>
      <title>DEV Community: shubhcode</title>
      <link>https://dev.to/shubhcode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shubhcode"/>
    <language>en</language>
    <item>
      <title>Implementing NFC in Android with Kotlin : Like Magic, But with Phones!</title>
      <dc:creator>shubhcode</dc:creator>
      <pubDate>Sat, 19 Aug 2023 12:07:34 +0000</pubDate>
      <link>https://dev.to/shubhcode/implementing-nfcin-android-withkotlin-like-magicbut-with-phones-2n0p</link>
      <guid>https://dev.to/shubhcode/implementing-nfcin-android-withkotlin-like-magicbut-with-phones-2n0p</guid>
      <description>&lt;p&gt;Hey there, fellow Android devs! Ever heard of NFC? No, it's not some secret society; its &lt;strong&gt;Near Field Communication&lt;/strong&gt; – the tech that lets your phone have a chat with another phone or a fancy tag when they're like, "Hey, I'm close!" Think of it as your phone's way of saying, "High-five, buddy!" 🤝📱&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NFC Unveiled:&lt;/strong&gt; Beyond Tap-to-Pay:&lt;br&gt;
So, NFC isn't just for tapping your phone to pay for stuff – it's like your phone's fancy way of doing secret handshakes with other devices. And guess what? You can make your Android app a part of this magic show!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting the NFC Party Started:&lt;/strong&gt;&lt;br&gt;
Ready to jump into the NFC pool? First things first, make sure your phone supports NFC, and ask for the permission nod in your AndroidManifest. Then, it's showtime – you've got two cool moves: reading and writing!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reading NFC like a Pro:&lt;/strong&gt;&lt;br&gt;
Imagine you're making an event app, and you want folks to check-in with a tap. NFC's got your back! Here's a simple breakdown with some Dev Humor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class EventCheckInActivity : AppCompatActivity() {

    private lateinit var nfcAdapter: NfcAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_check_in)

        nfcAdapter = NfcAdapter.getDefaultAdapter(this)
    }

    override fun onResume() {
        super.onResume()
        val pendingIntent = PendingIntent.getActivity(
            this, 0, Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0
        )
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null)
    }

    // Here comes the moment of truth!
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) {
            // Ta-da! Handle NFC data here
        }
    }
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Writing NFC Tags:&lt;/strong&gt;&lt;br&gt;
Time to let your app write messages to NFC tags. Imagine you're making a travel app – users tap their phone, and boom, their flight info is written to a tag. Look, it's like coding your own magic wand!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class WriteNFCActivity : AppCompatActivity() {

    private lateinit var nfcAdapter: NfcAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_write_nfc)

        nfcAdapter = NfcAdapter.getDefaultAdapter(this)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) {
            val tag = intent.getParcelableExtra&amp;lt;Tag&amp;gt;(NfcAdapter.EXTRA_TAG)
            val message = "Hey, I'm the NFC master!".toByteArray()
            val ndef = Ndef.get(tag)

            ndef.connect()
            ndef.writeNdefMessage(NdefMessage(message))
            ndef.close()

            // Tag has been written. Bam!
        }
    }
}

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

&lt;/div&gt;



&lt;p&gt;So there you have it, code buddies! NFC isn't some mystical spell; it's your ticket to adding some real-world magic to your apps. Whether you're crafting event check-ins, sharing sweet content, or just having a tech chat with tags, NFC's got your back. It's like coding with a sprinkle of Harry Potter charm! 🪄🔮&lt;/p&gt;

&lt;p&gt;Now go out there, tap into the NFC adventure, and let your Android app join the party. Happy tapping, fellow magicians! 🎩🌟🚀&lt;/p&gt;

&lt;p&gt;Remember, this blog is just your starting point on the road to NFC awesomeness. Have fun, explore, and let your creativity run wild with NFC in your Android apps – it's all about making tech fun, isn't it? 🤖🎉&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>androiddev</category>
      <category>nfc</category>
    </item>
    <item>
      <title>Coroutines: Making Async Less Stress, More Progress!</title>
      <dc:creator>shubhcode</dc:creator>
      <pubDate>Sat, 19 Aug 2023 07:08:10 +0000</pubDate>
      <link>https://dev.to/shubhcode/coroutines-making-async-less-stress-more-progress-32n</link>
      <guid>https://dev.to/shubhcode/coroutines-making-async-less-stress-more-progress-32n</guid>
      <description>&lt;p&gt;Hey fellow Android developers! Today, let's dive deep into a powerful tool that can significantly improve the way we handle asynchronous tasks in our apps - coroutines. You've probably heard about them, but I want to take you beyond the basics and explore some lesser-known aspects of coroutines that can truly elevate your Android development game. So grab your favorite cup of coffee, and let's get started!&lt;/p&gt;

&lt;p&gt;Understanding Coroutines:&lt;br&gt;
At its core, a coroutine is a concurrency design pattern that allows us to write asynchronous code in a more sequential and readable manner. Unlike callbacks or RxJava, coroutines let us write code that looks and feels like regular synchronous code, making it easier to reason about and maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Structured Concurrency:&lt;/strong&gt; One often overlooked aspect of coroutines is structured concurrency. This means that when you launch a coroutine in a specific scope, like an Activity or a ViewModel, it automatically gets cancelled when that scope is destroyed. No more memory leaks or unexpected behavior due to lingering background tasks!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyViewModel : ViewModel() {
    fun fetchData() {
        viewModelScope.launch {
            val data = fetchDataFromNetwork()
            updateUI(data)
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Exception Handling:&lt;/strong&gt; Coroutines provide a natural way of handling exceptions within asynchronous code. Unlike traditional try-catch blocks, we can wrap our asynchronous code in a try block, and exceptions will be propagated to the surrounding coroutine scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;viewModelScope.launch {
    try {
        val result = fetchData()
        updateUI(result)
    } catch (e: Exception) {
        showErrorDialog(e.message)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Cancellation and Timeout:&lt;/strong&gt; Coroutines make cancellation a breeze. You can cancel a coroutine by calling the cancel() function on its Job object. You can also set a timeout on a coroutine, ensuring that it doesn't run indefinitely.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val job = viewModelScope.launch {
    try {
        withTimeout(5000) {
            val data = fetchData()
            updateUI(data)
        }
    } catch (e: TimeoutCancellationException) {
        showErrorDialog("Request timed out")
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Custom Coroutine Contexts:&lt;/strong&gt; While we often use the default coroutine context provided by Android, you can create custom contexts tailored to your needs. This can be especially useful for handling different thread pools or enforcing specific behavior.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val customContext = newSingleThreadContext("CustomThread")
viewModelScope.launch(customContext) {
    // Your code here
}

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

&lt;/div&gt;



&lt;p&gt;So there you have it, dear developers! Coroutines are like the superheroes of async programming in the Android world. They swoop in, save you from callback chaos, and bring order to your code faster than you can say "NullPointerException" three times fast!&lt;/p&gt;

&lt;p&gt;With structured concurrency keeping things neat and tidy, exception handling that's smoother than your grandma's homemade pudding, and the power to cancel tasks before they start feeling too comfortable – coroutines have got your back.&lt;/p&gt;

&lt;p&gt;So go forth, my coding comrades, armed with the might of coroutines, and may your apps be bug-free and your coffee be strong! Happy coding! ☕🚀🦸‍♂️&lt;/p&gt;

&lt;p&gt;Follow me everywhere &lt;a class="mentioned-user" href="https://dev.to/shubhcode"&gt;@shubhcode&lt;/a&gt; &lt;/p&gt;

</description>
      <category>android</category>
      <category>androiddev</category>
      <category>kotlin</category>
      <category>coroutines</category>
    </item>
  </channel>
</rss>
