Android Interview Questions and Answers What is android?


Ordered Broadcast Intent Examples



Download 0.77 Mb.
View original pdf
Page51/62
Date17.12.2020
Size0.77 Mb.
#54986
1   ...   47   48   49   50   51   52   53   54   ...   62
Android Interview Questions
8.3 Ordered Broadcast Intent Examples
There are four BroadcastReceivers registered statically as follows




























android:name = "xper.example.ORDERED_BROADCAST_INTENT_TWO"/>

Their respective onReceive() methods are defined as follows
// OrderedBroadcastReceiverOne.onReceive() public void onReceive(Context context, Intent intent)
{
System.out.println("One: " + intent
System.out.println("\tgetResultCode() => " + getResultCode());
System.out.println("\tgetResultData() => " + getResultData()); setResultCode(getResultCode() + 1); setResultData(getResultData() + ", One
Bundle extras = getResultExtras(true); extras.putString("One.value", "One setResultExtras(extras);
System.out.println("One Done
}
// OrderedBroadcastReceiverTwoA.onReceive() public void onReceive(Context context, Intent intent)
{
System.out.println("TwoA");
System.out.println(intent);
System.out.println("\tgetResultCode() => " + getResultCode());
System.out.println("\tgetResultData() => " + getResultData()); setResultCode(getResultCode() + 20); setResultData(getResultData() + ", TwoA");
Bundle extras = getResultExtras(true); extras.putString("TwoA.value", "TwoA"); setResultExtras(extras); if ("xper.example.ORDERED_BROADCAST_INTENT_TWO".equals(intent.getAction()))
{ abortBroadcast();
}
System.out.println("TwoA done
}


// OrderedBroadcastReceiverTwoB.onReceive() public void onReceive(Context context, Intent intent)
{
System.out.println("TwoB");
System.out.println(intent);
System.out.println("\tgetResultCode() => " + getResultCode());
System.out.println("\tgetResultData() => " + getResultData()); setResultCode(getResultCode() + 20); setResultData(getResultData() + ", TwoB");
Bundle extras = getResultExtras(true); extras.putString("TwoB.value", "TwoB"); setResultExtras(extras); if ("xper.example.ORDERED_BROADCAST_INTENT_TWO".equals(intent.getAction()))
{ abortBroadcast();
}
System.out.println("TwoB done
}
// OrderedBroadcastReceiverThree.onReceive() public void onReceive(Context context, Intent intent)
{
System.out.println("Three");
System.out.println(intent);
System.out.println("\tgetResultCode() => " + getResultCode());
System.out.println("\tgetResultData() => " + getResultData()); setResultCode(getResultCode() + 300); setResultData(getResultData() + ", Three
Bundle extras = getResultExtras(true); extras.putString("Three.value", "Three setResultExtras(extras);
System.out.println("Three done
} There is a class ResultReceiver which is a subclass of the class BroadcastReceiver. Its onReceive() method is defined as follows. public void onReceive(Context context, Intent intent)
{


System.out.println("ResultReceiver");
System.out.println(intent);
System.out.println("\tgetResultCode() => " + getResultCode());
System.out.println("\tgetResultData() => " + getResultData());
Bundle extras = getResultExtras(true);
System.out.println("\tBegin Extras for (String key : extras.keySet())
{
System.out.print("\t\t");
System.out.print(key);
System.out.print("\t=> ");
System.out.println(extras.get(key));
}
System.out.println("\tEnd Extras
System.out.println("ResultReceiver done
}

Download 0.77 Mb.

Share with your friends:
1   ...   47   48   49   50   51   52   53   54   ...   62




The database is protected by copyright ©ininet.org 2024
send message

    Main page