(Android) Broadcast Intent Permissions

If you want to protect your broadcasts not to be received by other applications directly, one permission is required to define in the AndroidManifest.xml as follows,

 

<permission android:name="test.permission.intent"/>

 

Intent intentA= new Intent("test.intent.a");
sendBroadcast(intentA,"test.permission.intent"/);

 

If you only define this permission and not use this one, your app's receivers also can not receive the intents.

So if want to monitor these intents with permissions, we should add one uses-permission in the AndroidManifestxml.

 

<uses-permission android:name="test.permission.intent"/>

 

 

If you do not want to receive any intent outside of your application, "export = false" in the AndroidManifest.xml is helpful.

<receiver android:name="test.broadcastreceiver"

 android:exported="false">

 ......

< /receiver>

 

 

LocalBroadcastManager

Helper to register for and send broadcasts of Intents to local objects within your process.  This is has a number of advantages over sending global broadcasts with sendBroadcast(Intent)

  • You know that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data. 
  • It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes they can exploit. 
  • It is more efficient than sending a global broadcast through the system.

Android中,`sendBroadcast()` 方法用于向所有注册了特定广播接收器的应用发送一条广播信息。如果你需要发送广播并要求权限,通常是在应用启动或者某些操作完成之后。这里有一个简单的示例: 首先,你需要在 AndroidManifest.xml 文件里声明需要使用的广播意图(intent)以及相应的权限: ```xml <uses-permission android:name="android.permission.BROADCAST_STICKY" /> <!-- 如果有其他自定义权限 --> <!-- <uses-permission android:name="your.package.permission.YOUR_PERMISSION_NAME" /> --> <intent-filter> <action android:name="com.example.YOUR_ACTION" /> </intent-filter> ``` 然后,在你的 Activity 或者 Service 中发送广播: ```java import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; public class YourActivity extends AppCompatActivity { private static final String ACTION_YOUR_ACTION = "com.example.YOUR_ACTION"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 请求权限(如果在Manifest文件中只写了BROADCAST_STICKY,此处不需要) if (ContextCompat.checkSelfPermission(this, Manifest.permission.BROADCAST_STICKY) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BROADCAST_STICKY}, REQUEST_SEND_BROADCAST); } else { sendBroadcast(new Intent(ACTION_YOUR_ACTION)); } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case REQUEST_SEND_BROADCAST: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 用户同意权限后,再次尝试发送广播 sendBroadcast(new Intent(ACTION_YOUR_ACTION)); } else { // 权限请求被拒绝,处理这种情况 Toast.makeText(this, "缺少广播权限", Toast.LENGTH_SHORT).show(); } break; } } // 创建一个简单的 BroadcastReceiver 接收广播 private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (ACTION_YOUR_ACTION.equals(intent.getAction())) { // 在这里处理接收到的广播数据 Log.d("BroadcastReceiver", "Received action: " + intent.getAction()); } } }; @Override protected void onResume() { super.onResume(); registerReceiver(broadcastReceiver, new IntentFilter(ACTION_YOUR_ACTION)); } @Override protected void onPause() { super.onPause(); unregisterReceiver(broadcastReceiver); } } ``` 在这个例子中,当你获得 `BROADCAST_STICKY` 的权限后,会发送一个包含自定义动作 (`ACTION_YOUR_ACTION`) 的广播,并且在 `onResume()` 中注册一个接收器以便监听这个广播。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值