方法中启动的Activity动态注册广播,此新启动的Activity接收不到此方法后续发出的广播

今天写音乐播放器歌词展示界面(这个播放器的代码可能周末再释放出来吧,有点乱,需要整理),本来想点击列表框后弹出展示歌词Activity,同时接收方法后续代码中发出的广播,但却一直接收不到,调整几次后,觉得可能方法X中新启动Activity,在其这个新的Activity中的onCreate方法动态注册广播,此时这个新启动的Activity接收不到此方法X后续发出的广播
最终测试发现果真如此
测试代码如下:

 1     package com.example.myfirstapp;
 2 
 3     import android.app.Activity;
 4     import android.content.Intent;
 5     import android.os.Bundle;
 6     import android.view.View;
 7 
 8     public class FirstActivity extends Activity {
 9         
10         /* (非 Javadoc)
11          * Title: onCreate
12          * Description:
13          * @param savedInstanceState
14          * @see android.app.Activity#onCreate(android.os.Bundle)
15          */
16         @Override
17         protected void onCreate(Bundle savedInstanceState) {
18             super.onCreate(savedInstanceState);
19             setContentView(R.layout.firstactivity);
20             System.out.println("SecondActivity");
21         }
22         
23         public void openActivity2( View view ) {
24             Intent mIntent = new Intent(this, SecondActivity.class);
25             startActivity(mIntent);
26             Intent broadcastIntent = new Intent("com.example.firstactivity");
27             sendBroadcast(broadcastIntent);
28         }
29         
30     }
FirstActivity

布局:

 1     <?xml version="1.0" encoding="utf-8"?>
 2     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3         android:layout_width="match_parent"
 4         android:layout_height="match_parent"
 5         android:orientation="vertical" >
 6 
 7         <Button
 8             android:layout_width="fill_parent"
 9             android:layout_height="wrap_content"
10             android:onClick="openActivity2"
11             android:text="Open" />
12 
13     </LinearLayout>
firstactivity

SecondActivity中的代码:

 1     package com.example.myfirstapp;
 2 
 3     import java.util.Date;
 4 
 5     import android.app.Activity;
 6     import android.content.BroadcastReceiver;
 7     import android.content.Context;
 8     import android.content.Intent;
 9     import android.content.IntentFilter;
10     import android.os.Bundle;
11     import android.view.View;
12     import android.widget.TextView;
13 
14     public class SecondActivity extends Activity {
15         
16         private TextView second_tv1 = null;
17         private SecondBoradCast mSecondBoradCast = new SecondBoradCast();
18         
19         /* (非 Javadoc)
20          * Title: onCreate
21          * Description:
22          * @param savedInstanceState
23          * @see android.app.Activity#onCreate(android.os.Bundle)
24          */
25         @Override
26         protected void onCreate(Bundle savedInstanceState) {
27             super.onCreate(savedInstanceState);
28             setContentView(R.layout.secondactivity);
29             second_tv1 = (TextView)findViewById(R.id.second_tv1);
30             registerReceiver(mSecondBoradCast, new IntentFilter("com.example.firstactivity"));
31             System.out.println("SecondActivity");
32         }
33         
34         public void sendBroadCast( View view ) {
35             Intent broadcastIntent = new Intent("com.example.firstactivity");
36             sendBroadcast(broadcastIntent);
37         }
38         
39         /* (非 Javadoc)
40          * Title: onDestroy
41          * Description:
42          * @see android.app.Activity#onDestroy()
43          */
44         @Override
45         protected void onDestroy() {
46             super.onDestroy();
47             if( mSecondBoradCast != null ) {
48                 unregisterReceiver(mSecondBoradCast);
49                 mSecondBoradCast = null;
50             }
51         }
52         
53         public class SecondBoradCast extends BroadcastReceiver {
54 
55             /* (非 Javadoc)
56              * Title: onReceive
57              * Description:
58              * @param context
59              * @param intent
60              * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
61              */
62             @Override
63             public void onReceive(Context context, Intent intent) {
64                 System.out.println(new Date());
65                 second_tv1.setText("接收成功");
66             }
67             
68         }
69         
70     }
SecondActivity

布局:

 1     <?xml version="1.0" encoding="utf-8"?>
 2     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3         android:layout_width="match_parent"
 4         android:layout_height="match_parent"
 5         android:orientation="vertical" >
 6 
 7         <Button
 8             android:layout_width="fill_parent"
 9             android:layout_height="wrap_content"
10             android:onClick="sendBroadCast"
11             android:text="Send" />
12 
13         <TextView
14             android:id="@+id/second_tv1"
15             android:layout_width="fill_parent"
16             android:layout_height="fill_parent"
17             android:text="Just Test"
18             android:textColor="#f000" />
19 
20     </LinearLayout>
secondactivity

运行此项目,点击第一个Activity中的Open按钮后,出现界面

点击Send按钮,发送广播,界面中的TextView改变文字

在点击了Send按钮之后,才接收到广播

原文地址:https://www.cnblogs.com/creasylai19/p/3771562.html