Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VA对Broadcast的处理讨论及分析 #9

Open
prife opened this issue Aug 24, 2016 · 2 comments
Open

VA对Broadcast的处理讨论及分析 #9

prife opened this issue Aug 24, 2016 · 2 comments

Comments

@prife
Copy link
Owner

prife commented Aug 24, 2016

问题描述:假设两个应用A和B,A中发送某广播,B中接收。A、B都安装到了系统中,即只讨论双开模式。那下面有如下问题,

  1. 如果A安装到了VA中,B没有,那么运行A发送广播,B是否应该收到?
  2. 如果A、B都安装到了VA中。运行A,那么应该是系统B收到还是VA里的B收到,还是都收到?

目前VA Master分支最新代码,这两种下B(无论是否安装到VA中)都不能收到A发送的广播。

对比平行空间:

  1. 系统中B能收到广播
  2. 平行空间中的B收到平行空间中A发送的广播。

因为VA还支持插件模式,这个问题就变的比较复杂。

@prife
Copy link
Owner Author

prife commented Aug 24, 2016

设计了如下demo,应用A按下按钮发送广播(携带一段字符串),应用B静态注册广播,在onReceive函数中启动一个Activity并把广播中的字符串显示在TextView控件上。

应用A代码

一个helleworld应用,有个button,按下button时,即发送。

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.send_broadcast);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //String name = "study.broadcast.test";
                String name = "prife.test";
                String value = "hello world";
                Intent intent = new Intent(name);
                intent.putExtra("prife_value", value);
                Log.d(TAG, String.format("send action[%s], value[%s]", name, value));
                sendBroadcast(intent);
            }
        });
    }

应用B代码

广播接收应用B,当收到广播(action为prife.test),则启动一个Activity(ReceiveBroadCast.java),并讲Intent中携带的字符串显示出来,主要代码如下。

AndroidManifest.xml

        <receiver android:name=".Receiver" >
            <intent-filter>
                <!--<action android:name="study.broadcast.test" />-->
                <action android:name="prife.test" />
            </intent-filter>
        </receiver>
        <activity android:name=".ReceiveBroadCast">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

Receiver.java

public class Receiver extends BroadcastReceiver {

    static final String TAG = "Receiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String values = intent.getStringExtra("prife_value");
        Log.d(TAG, String.format("action[%s], value[%s]", action, values));

        Intent intent1 = new Intent(context, ReceiveBroadCast.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent1.putExtra("value", values);
        context.startActivity(intent1);
  }
}

ReceiveBroadCast.java

public class ReceiveBroadCast extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receivebroad);

        Intent intent = getIntent();
        String value = intent.getStringExtra("value");
        TextView view = (TextView)findViewById(R.id.receive_broadcast);
        view.setText(value);
    }
}

@cddjr
Copy link

cddjr commented Nov 10, 2016

其实只要在广播前先判断接收器是否已经在VA中注册,如果没有注册那么广播不做任何修改直接传给系统,这样就实现了场景1。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants