標籤:
粘滯廣播:廣播發送出去以後,廣播接收者還沒有建立,當廣播接收者註冊的時候就可以接收,如果不是粘滯廣播則如果沒有廣播接收者就以後不能再接收了。
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 粘滯廣播