android 52 粘滞广播

粘滞广播:广播发送出去以后,广播接收者还没有创建,当广播接收者注册的时候就可以接收,如果不是粘滞广播则如果没有广播接收者就以后不能再接收了。

 mainActivity:

package com.sxt.day07_07;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent=new Intent("com.sxt.day07_07.my_receiver");
        sendStickyBroadcast(intent);//发送粘滞广播,一直停留等着接收者
        setListener();
    }

    private void setListener() {
        findViewById(R.id.btnStartSecondActivity).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2=new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent2);//启动SecondActivity
            }
        });
    }

}

SecondActivity

package com.sxt.day07_07;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;

public class SecondActivity extends Activity {
    MyReceiver mReceiver;
    Intent mIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        registerReceiver();
    }
    private void registerReceiver() {
        mReceiver=new MyReceiver();
        IntentFilter filter=new IntentFilter("com.sxt.day07_07.my_receiver");
        registerReceiver(mReceiver, filter);
    }
    
    //内部类,只有SecondActivity启动了,MyReceiver才能注册接收广播。
    class MyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("main","MyReceiver.onReceive()");
            mIntent=intent;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeStickyBroadcast(mIntent);//移出,移出以后就收不到广播了
        unregisterReceiver(mReceiver);//移出接收者
    }
}

系统描述文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sxt.day07_07"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY"/>  粘滞广播要申请权限
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.sxt.day07_07.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>
        <activity
            android:name="com.sxt.day07_07.SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>
    </application>

</manifest>

main页面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnStartSecondActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

second页面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SecondActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

 

posted @ 2015-10-19 21:18  无天666  阅读(385)  评论(0编辑  收藏  举报