Skip to content

Commit de5d6cf

Browse files
committed
增加对动态注册的Receiver的测试代码
1 parent eb02038 commit de5d6cf

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

PluginMain/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
1212
<uses-permission android:name="android.permission.INTERNET"/>
13+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1314

1415
<application
1516
android:name="com.plugin.core.PluginApplication"
364 Bytes
Binary file not shown.

PluginTest/src/com/example/plugintest/activity/PluginTestOpenPluginActivity.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.example.plugintest.activity;
22

33
import android.app.Activity;
4+
import android.content.BroadcastReceiver;
5+
import android.content.Context;
46
import android.content.Intent;
7+
import android.content.IntentFilter;
58
import android.os.Bundle;
69
import android.util.Log;
710
import android.view.View;
@@ -15,6 +18,8 @@
1518

1619
public class PluginTestOpenPluginActivity extends Activity implements OnClickListener {
1720

21+
NestReceiver nestReceiver;
22+
1823
@Override
1924
public void onCreate(Bundle savedInstanceState) {
2025
super.onCreate(savedInstanceState);
@@ -23,7 +28,19 @@ public void onCreate(Bundle savedInstanceState) {
2328
setContentView(btn);
2429
btn.setOnClickListener(this);
2530

26-
Log.d("paramVO", ((SharePOJO)getIntent().getSerializableExtra("paramVO")).name);
31+
Log.d("paramVO", ((SharePOJO) getIntent().getSerializableExtra("paramVO")).name);
32+
33+
IntentFilter testFiler = new IntentFilter();
34+
testFiler.addAction("xx.nest");
35+
nestReceiver = new NestReceiver();
36+
registerReceiver(nestReceiver, testFiler);
37+
}
38+
39+
class NestReceiver extends BroadcastReceiver {
40+
@Override
41+
public void onReceive(Context context, Intent intent) {
42+
Log.d("xx", ((ParamVO) intent.getSerializableExtra("paramvo")) + ", action:" + intent.getAction());
43+
}
2744
}
2845

2946
@Override
@@ -69,6 +86,19 @@ public void onClick(View v) {
6986
pvo.name = "打开PluginTestReceiver";
7087
intent.putExtra("paramvo", pvo);
7188
sendBroadcast(intent);
89+
90+
//测试动态注册的插件广播
91+
intent = new Intent("xx.nest");
92+
intent.putExtra("str1", "打开动态注册的NestReceiver——————");
93+
pvo = new ParamVO();
94+
pvo.name = "打开动态注册的NestReceiver";
95+
intent.putExtra("paramvo", pvo);
96+
sendBroadcast(intent);
7297
}
7398

99+
@Override
100+
protected void onDestroy() {
101+
super.onDestroy();
102+
unregisterReceiver(nestReceiver);
103+
}
74104
}

PluginTest/src/com/example/plugintest/receiver/PluginTestReceiver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import com.plugin.util.LogUtil;
1212

1313
/**
14-
* 插件receiver不能监听系统广播
14+
* 静态注册的插件receiver不能监听系统广播
15+
*
1516
* @author cailiming
1617
*
1718
*/

PluginTest/src/com/example/plugintest/receiver/PluginTestReceiver2.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import android.content.BroadcastReceiver;
44
import android.content.Context;
55
import android.content.Intent;
6+
import android.util.Log;
67
import android.widget.Toast;
78

89
import com.example.plugintest.R;
10+
import com.example.plugintest.vo.ParamVO;
911

1012
/**
11-
* 插件receiver不能监听系统广播
13+
* 静态注册的插件receiver不能监听系统广播
1214
*
1315
* @author cailiming
1416
*
@@ -17,7 +19,6 @@ public class PluginTestReceiver2 extends BroadcastReceiver {
1719

1820
@Override
1921
public void onReceive(Context context, Intent intent) {
20-
2122
Toast.makeText(context, "PluginTestReceiver2 onReceive " + context.getResources().getText(R.string.hello_world4), Toast.LENGTH_LONG).show();
2223
}
2324

0 commit comments

Comments
 (0)