My requirement is, from one APP I want to notify another APP some events with broadcast, but I don't want the other APPs also receive the broadcast. So I think sendOrderedBroadcast with permission is a good solution.
Then my test looks like this:
BCSender APP:
which will define a custom permission, and send broadcast with this permission:
<permission
android:protectionLevel="normal"
android:name="com.czou.permission.BC" />
The only activity in BCSender:
package com.example.bcsender;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
private static String BC_PERMISSION = "com.czou.permission.BC";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBroadcast();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void sendBroadcast()
{
Intent intent = new Intent("android.intent.action.czou_broadcast");
intent.putExtra("msg", "hello from czou");
sendOrderedBroadcast(intent, BC_PERMISSION);
//sendBroadcast(intent, BC_PERMISSION);
}
}
BCReceiverA APP:
Which will declare the permission defined in BCSender APP and register broadcast receiver:
<user-permission android:name="com.czou.permission.BC" />
The only activity in BCReceiverA:
package com.example.bcreceivera;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
private static String TAG = "BCReceiverA";
private BroadcastReceiver mReceiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onDestroy()
{
super.onDestroy();
unregisterReceiver();
}
private void registerReceiver()
{
android.util.Log.d(TAG, "register receiver.");
mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String msg = intent.getStringExtra("msg");
android.util.Log.d(TAG, "Receive broadcast. " + msg);
Bundle bundle = new Bundle();
bundle.putString("msg", msg + "pass through ReceverA");
setResultExtras(bundle);
abortBroadcast();
}
};
IntentFilter intentFilter = new IntentFilter("android.intent.action.czou_broadcast");
intentFilter.setPriority(1000);
this.registerReceiver(mReceiver, intentFilter);
}
private void unregisterReceiver()
{
android.util.Log.d(TAG, "unregister receiver.");
this.unregisterReceiver(mReceiver);
}
}
You can find, in BCSender, I use sendOrderedBroadcast(intent, BC_PERMISSION)
, the result is that BCReceiverA can not receive the broadcast, neither sendBroadcast(intent, BC_PERMISSION)
. But if I use sendBroadcast(intent)
, receiver can get it.
So what's wrong with sendBroadcast()/sendOrderedBroadcast()
here?
You have <user-permission>
. The element name is supposed to be <uses-permission>
.
Also, you may wish to declare the <permission>
element in both apps, so the order of their installation does not matter. Otherwise, the <permission>
app has to be installed before the <uses-permission>
app.