1. 程式人生 > >BroadcastReceiver(一)在AndroidManifest.xml中配置的廣播接收器(自動註冊登出)

BroadcastReceiver(一)在AndroidManifest.xml中配置的廣播接收器(自動註冊登出)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fk.lbroadcastreceiver" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/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>

        <!-- 註釋下面程式碼,則Broadcast改為手動註冊登出 -->
         <receiver 
         android:name=".LReceiver" 
         android:enabled="true" 
         android:exported="true" > 
         </receiver> 
       
    </application>

</manifest>

2. Receiver程式碼

public class LReceiver extends BroadcastReceiver {


    public LReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        //throw new UnsupportedOperationException("Not yet implemented");

        System.out.println("接收器L 收到了訊息:"  + intent.getStringExtra("msg"));
    }
}