如何使用自定义权限sendOrderedBroadcast到其他APP?

时间:2013-03-29 07:17:57

标签: android permissions broadcastreceiver broadcast

我的要求是,从一个APP我想通过广播通知另一个APP一些事件,但我不希望其他APP也接收广播。所以我认为sendOrderedBroadcast经过许可是一个很好的解决方案。

然后我的测试看起来像这样:

BCSender APP:
,它将定义自定义权限,并使用此权限发送广播:

<permission 
    android:protectionLevel="normal"
    android:name="com.czou.permission.BC" />

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:
这将声明BCSender APP中定义的权限并注册广播接收者:

<user-permission android:name="com.czou.permission.BC" />

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);     
    }    
}


您可以在BCSender中找到sendOrderedBroadcast(intent, BC_PERMISSION),结果是BCReceiverA无法接收广播,sendBroadcast(intent, BC_PERMISSION)。但是如果我使用sendBroadcast(intent),接收者就可以得到它。

那么sendBroadcast()/sendOrderedBroadcast()出现了什么问题?

1 个答案:

答案 0 :(得分:4)

你有<user-permission>。元素名称应为<uses-permission>

此外,您可能希望在两个应用中声明<permission>元素,因此其安装顺序无关紧要。否则,<permission>应用 安装在 <uses-permission>应用之前