protected-broadcast的作用

转载自http://www.cnblogs.com/caidi/p/4513825.html 


“保护性广播”,在一些AndroidManifest.xml中的一级标记<protected-broadcast>,具体有何作用:

此处指定一个广播,该广播只能被系统发送。
注 意:只有系统appliaction才能在其AndroidManifest.xml中定义Protected Broadcast,系统appliaction包括/system/framework、/system/app、vendor/app下的 package,因此设备中安装的第三方apk中如果定义了Protected Broadcast,那么这个Protected Broadcast将不生效。

 

简单地说,Android认为有一些广播是只能由系统发送的,如果某个系统级AndroidManifest.xml中写了这个标记,那么在PKMS解析该文件时,就会把“保护性广播”标记中的名字(一般是Action字符串)记录下来。

在系统运作起来之后,如果某个不具有系统权限的应用试图发送系统中的“保护性广播”,AMS会抛出异常,提示"Permission Denial: not allowed to send broadcast"。

代码中可以看到<protected-broadcast>标记的具体写法,如下

<protected-broadcast android:name="android.intent.action.SERVICE_STATE" />
<protected-broadcast android:name="android.intent.action.RADIO_TECHNOLOGY" />


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
用JAVA语言实现网络聊天import java.io.*; import java.net.*; import java.util.*; public class ChatHandler implements Runnable { protected Socket socket; protected ObjectInputStream dataIn; protected ObjectOutputStream dataOut; protected Thread listener; protected static Vector handlers = new Vector(); private boolean keepListening = true; public ChatHandler(Socket socket) { this.socket = socket; } public synchronized void start() { if (listener == null) { try { dataIn = new ObjectInputStream(socket.getInputStream()); dataOut = new ObjectOutputStream(socket.getOutputStream()); listener = new Thread(this); listener.start(); } catch (IOException ioException) { ioException.printStackTrace(); } } } public synchronized void stop() { if (listener != null) { try { if (listener != Thread.currentThread()) listener.interrupt(); listener = null; dataOut.close(); socket.close(); } catch (IOException ignored) { } } } public void run() { String message = ""; try { handlers.addElement(this); while (keepListening) { message = (String) dataIn.readObject(); if (message.equals("DISCONNECT")) { dataOut.writeObject(message); dataOut.flush(); stopListening(); } else broadcast(message); } } catch (ClassNotFoundException classNotFoundException) { } catch (EOFException ignored) { } catch (IOException ex) { if (listener == Thread.currentThread()) ex.printStackTrace(); } finally { handlers.removeElement(this); } try { dataIn.close(); } catch (IOException ioException) { ioException.printStackTrace(); } stop(); } protected void broadcast(String message) { synchronized (handlers) { Enumeration enumer = handlers.elements(); while (enumer.hasMoreElements()) { ChatHandler handler = (ChatHandler) enumer.nextElement(); try { handler.dataOut.writeObject(message); handler.dataOut.flush(); } catch (IOException ex) { handler.stop(); } } } } public void stopListening() { keepListening = false; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值