How to communicate between mobile phone programs:
Broadcast-Receiver mechanism
Listening :
The broadcast receiver needs to be implemented as a subclass of the BroadcastReceiver class, and override the onReceive() method to receive messages with Intent objects as parameters
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
Register listener: AndroidManifest.xml
<application
android:icon="@drawable/ ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="android. intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
Important System Events
android.intent.action.BATTERY_CHANGED Persistent broadcast containing battery state of charge, level and other information.
android.intent.action.BATTERY_LOW Identifies a low battery condition for the device.
android.intent.action.BATTERY_OKAY Indicates that the battery is now OK after being low.
android.intent.action.BOOT_COMPLETED is broadcast once after the system has finished booting.
android.intent.action.BUG_REPORT Displays the activity reporting bugs.
android.intent.action.CALL executes a call to someone specified by the data.
android.intent.action.CALL_BUTTON The user clicks the "call" button to open the dialer or other suitable interface for dialing.
android.intent.action.DATE_CHANGED Date changed.
android.intent.action.REBOOT Device reboot.
Send custom event:
public void broadcastIntent(View view)
{
Intent intent = new Intent();
intent.setAction("cn.uprogrammer.CUSTOM_INTENT");
sendBroadcast(intent);
}
lesson 5 Programs communicate with each other-Broadcast-Receiver
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326091992&siteId=291194637
Recommended
Ranking