Android BroadcastReceiver and broadcastreceiver
The Android broadcast mechanism has three basic elements:
Broadcast Sender (call the sendBroadcast method)-used to send broadcast;
BroadcastReceiver-used to receive broadcasts;
Intent content (Intent)-media used to save broadcast-related information.
Broadcast is a widely used mechanism for Android to directly transmit information between applications or between application components. BroadcastReceiver is a component that filters and accepts and responds to the sent broadcast Intent.
BroadcastReceiver is used to receive broadcast Intent asynchronously. The broadcast Intent is sent by calling Context. sendBroadcast. Generally, a broadcast Intent can be received by multiple broadcast recipients subscribed to this Intent.
After each broadcast Intent is sent, the system will create the corresponding BroadcastReceiver instance and automatically trigger its onReceiver () method. After the onReceiver () method is executed, the BroadcastReveiver instance is destroyed.
Generally, the life cycle of a BroadcastReceiver object cannot exceed 5 seconds, so some time-consuming operations cannot be performed in BroadcastReceiver. To perform a time-consuming operation based on Broadcast, you can use Intent to start a Service.
Instance: BroadcastDemo
Running effect:
Code List:
AndroidManifest. xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rainsong.broadcastdemo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" /> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> <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="com.rainsong.action.NEW_BROADCAST" /> </intent-filter> </receiver> </application></manifest>
Layout file: main. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <Button android: id = "@ + id/button1" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Send broadcast"/> </LinearLayout>
Java source code file: MainActivity. java
package com.rainsong.broadcastdemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity{ public final String ACTION = "com.rainsong.action.NEW_BROADCAST"; Button btn1; OnClickListener listener1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1 = (Button)findViewById(R.id.button1); listener1 = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(ACTION); sendBroadcast(intent); } }; btn1.setOnClickListener(listener1); }}
Java source code file: MyReceiver. java
package com.rainsong.broadcastdemo;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class MyReceiver extends BroadcastReceiver { public static int NOTIFICATION_ID = 12345; @Override public void onReceive(Context context, Intent intent) { NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(android.R.drawable.stat_notify_chat, "MyReceiver onReceive", System.currentTimeMillis()); Intent intent1 = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1, 0); notification.setLatestEventInfo(context, "MyReceiver onReceive", null, pendingIntent); nm.notify(NOTIFICATION_ID, notification); }}
Code logic:
Click the send broadcast button. Our program starts to send the broadcast Intent. This broadcast Intent is intercepted by MyReceiver, and then runs the onReceive method in MyReceiver er, in onReceive, a Notification is displayed in the status bar.