You are on page 1of 13

Mobile Computing

Miscellaneous Topics of Mobile


Computing
Broadcast Receivers
• Apps can send or receive broadcast messages
from:
– the Android system
– other Android apps
• The broadcasts are sent when an event of
interest occurs, for examples
– system boots up
– device starts charging
• Apps can also send custom broadcasts
• Apps can register to receive specific broadcasts
Platform Specific Limitations
• Android 9 (API level 28) and above
– The NETWORK_STATE_CHANGED_ACTION broadcast
doesn't receive information about the user's location or
personally identifiable data.
– broadcasts from Wi-Fi don't contain SSIDs, BSSIDs,
connection information, or scan results.
• Android 8 (API level 26) and above
– cannot use the manifest to declare a receiver for most
implicit broadcasts
• Android 7 (API level 24) and above
– don't send the following system broadcasts:
• ACTION_NEW_PICTURE
• ACTION_NEW_VIDEO
– Some manifest declaration does not work, for an example
• CONNECTIVITY_ACTION
Manifest-declared receivers
• the system launches your app when the
broadcast is sent.

<receiver android:name=".MyBroadcastReceiver" android:exported="true">


<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
</intent-filter>
</receiver>
Manifest-declared receivers Cont.
• displays the contents of the broadcast:

public class MyBroadcastReceiver extends BroadcastReceiver {


private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
StringBuilder sb = new StringBuilder();
sb.append("Action: " + intent.getAction() + "\n");
sb.append("URI: " +
intent.toUri(Intent.URI_INTENT_SCHEME).toString() + "\n");
String log = sb.toString();
Log.d(TAG, log);
Toast.makeText(context, log, Toast.LENGTH_LONG).show();
}
}
Context-registered receivers
• Context-registered receivers receive broadcasts as long as their
registering context is valid.
– Activity context: valid till the activity is destroyed
– Application context: valid till the application is destroyed
• Create an instance of BroadcastReceiver:

BroadcastReceiver br = new MyBroadcastReceiver();

• register the receiver


IntentFilter filter = new
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
this.registerReceiver(br, filter);
Stop receiving broadcasts, call
• Be sure to unregister the receiver when
– The app no longer need it
– the context is no longer valid.

unregisterReceiver(android.content.BroadcastReceiver)
Sending Broadcasts
• The three ways for apps to send broadcast:
1. sendOrderedBroadcast(Intent, String) –
– sends broadcasts to one receiver at a time
– It can completely abort the broadcast
– Order can be set by setting the priority
2. sendBroadcast(Intent) –
– sends broadcasts to all receivers in an undefined
order
– receivers cannot read results from other receivers
3. LocalBroadcastManager.sendBroadcast –
– sends broadcasts to receivers that are in the same
app as the sender
Sending Broadcasts

Intent intent = new Intent();


intent.setAction("com.example.broadcast.MY_NOTIFICATION");
intent.putExtra("data","Notice me please!");
sendBroadcast(intent);
Send/Receive with permissions
• Sending
sendBroadcast(new Intent("com.example.NOTIFY"),
Manifest.permission.SEND_SMS);
<uses-permission android:name="android.permission.SEND_SMS"/>

• Receiving
<receiver android:name=".MyBroadcastReceiver"
android:permission="android.permission.SEND_SMS">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
IntentFilter filter = new
IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(receiver, filter,
Manifest.permission.SEND_SMS, null );
Keep the device awake
• The following two methods do not require any special permission.

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.
FLAG_KEEP_SCREEN_ON);
}
}

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
Keep the CPU on
• The following permission is required.
<uses-permission android:name="android.permission.WAKE_LOCK" />

• To keep the CPU on:


PowerManager powerManager = (PowerManager)
getSystemService(POWER_SERVICE);
WakeLock wakeLock =
powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyApp::MyWakelockTag");
wakeLock.acquire();

• To release:
wakeLock.release();
Other Topics
• OpenGL ES
• Audio and Video
• Biometric Authentication
• Touch and input
• CameraX
• Web based content
• Google play services
• Android Games

You might also like