android 52 粘滯廣播

來源:互聯網
上載者:User

標籤:

粘滯廣播:廣播發送出去以後,廣播接收者還沒有建立,當廣播接收者註冊的時候就可以接收,如果不是粘滯廣播則如果沒有廣播接收者就以後不能再接收了。

 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>

 

android 52 粘滯廣播

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.