-
Notifications
You must be signed in to change notification settings - Fork 187
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
Comments
设计了如下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为 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);
}
} |
其实只要在广播前先判断接收器是否已经在VA中注册,如果没有注册那么广播不做任何修改直接传给系统,这样就实现了场景1。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
问题描述:假设两个应用A和B,A中发送某广播,B中接收。A、B都安装到了系统中,即只讨论双开模式。那下面有如下问题,
目前VA Master分支最新代码,这两种下B(无论是否安装到VA中)都不能收到A发送的广播。
对比平行空间:
因为VA还支持插件模式,这个问题就变的比较复杂。
The text was updated successfully, but these errors were encountered: