Broadcast Receiver (3)



BroadcastReceiver အသံုးျပဳ ျခင္း (3)
===========================
ဒီတခါေတာ့ charger တပ္တာ၊ ျဖဳတ္တာ action ၂မ်ိဳးကို ေစာင့္နားေထာင္တဲ့
BroadcastReceiver ကို ေရးျပထားပါတယ္။
MyReceiver.java
==============
package com.mycompany.myapp;
import android.content.*;
import android.widget.*;
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(action.equals(Intent.ACTION_POWER_CONNECTED)) {
Toast.makeText(context, "အားသြင္းႀကိဳး တပ္လိုက္တယ္"
,
Toast.LENGTH_SHORT).show();
}
else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
Toast.makeText(context, "အားသြင္းႀကိဳး ျဖဳတ္လိုက္တယ္"
,Toast.LENGTH_SHORT).show();
}
}
}
============


AndroidManifest.java
=================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="MyReceiver">
<intent-filter>
<action
android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action
android:name="android.intent.action.ACTION_POWER_DISCONNECTED"
/>
</intent-filter>
</receiver>
</application>
</manifest>
==============
App ကို run လိုက္ပါ။ အားသြင္းႀကိဳးကို တပ္ၾကည့္ပါ။ ျဖဳတ္ၾကည့္ပါ။ Toast ေလး နဲ႔
တပ္ေၾကာင္း၊ ျဖဳတ္ေၾကာင္း ျပပါလိမ့္မယ္။
===========
ဆက္လက္ ေဆြးေႏြးေပးပါမယ္။

#nnl


AIDE Android Lessons And Project Group
ဆရာ Nyi Nyi Lwin Post မွ ကူးယူတင္ထားပါသည္

#Credit ဆရာ Nyi Nyi Lwin


Comments

Popular posts from this blog

♡ မဂၤလာခြန္းဆက္ ♡

《《 ဝမ္းနည္းပါေၾကာင္း 》》