Android短信Receiver优先级

本文探讨了Android平台上短信广播接收器的优先级问题,通过实验对比了静态与动态注册的广播接收器之间的优先级差异,并详细分析了不同情况下广播接收器优先级的影响因素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android上的一些应用都有拦截短信广播的功能,360,各种手机卫士,还有一些通讯录。最恼人的就是通讯录这些,有的甚至是拦截短信,扔掉广播,由它帮你入库。

经过反编译,有点眉目。360,金山手机卫士的manifest里面根本就没有注册短息的Receiver,所以他们只可能是动态注册短信广播接收器。

还有这个东西:

<intent-filter android:priority="2147483647">
优先级他们都会设置成这个很长的int,其实这个数是最大int型整数。


我在网上看到过一些,说是动态注册的广播接收器优先级高于静态注册,此时便很清楚了。

我们可以测试一下,写一个开机启动的Receiver:

<receiver android:name=".BootReceiver" >
     <intent-filter android:priority="2147483647" >
           <action android:name="android.intent.action.BOOT_COMPLETED" />
     </intent-filter>
</receiver>


在onreceiver里面启动一个service:

	@Override
	public void onReceive(Context context, Intent intent) {
		Intent intent2 = new Intent();
		intent2.setClass(context, SmsService.class);
		context.startService(intent2);
	}


在这个service里面动态注册短息的广播接收器:

public class SmsService extends Service {
	private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		IntentFilter filter = new IntentFilter(ACTION);
		filter.setPriority(2147483647);
		MyBrocast myService = new MyBrocast();
		registerReceiver(myService, filter);
	}

	private class MyBrocast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			System.out.println("receiver message --->>>>");
			abortBroadcast();
		}

	}

}
运行程序,然后重启,给模拟器发送短信,结果是测试程序给拦掉了,金山,360没有反映。


说下这个有序广播的优先级问题。以下有部分我没有测试过,也是四处看的,如果有错,请您纠正。

动态注册优先级别高于静态注册

在动态注册中
最早动态注册优先级别最高

在静态注册中
最早安装的程序,静态注册优先级别最高(安装APK会解析manifest.xml,把其加入队列)
这里安装的应用不包括rom里面的应用。
然后才是adb push到其他目录的应用。
可能的原因是手机查询应用的时候会先去特定目录解析应用,所以广播注册会出现这种差别。adb push 到system/app下会比安装的优先级高吗?这有待验证,我还没测试过。
然后都是安装的应用中,首先安装的优先等级最高。

个人认为这种有序广播存在很大的不足,导致大量第三方应用程序滥用方法去抢夺广播优先级。

暂时就这么多了……





                
@SuppressLint("NewApi") public class MainActivity extends Activity { SmsReceiver myReceiver; Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { test(); } }); } public void test(){ Cursor cursor = null; String defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(this); Intent intent = new Intent(Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, this.getPackageName()); startActivity(intent); try { cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[] { "_id", "address", "read" }, "read = ? ", new String[] {"0" }, "date desc"); if (cursor != null) { ContentValues values = new ContentValues(); values.put("read", "1"); for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { Log.v("cky", "" + cursor.getInt(cursor.getColumnIndex("_id")) + " , " + cursor.getString(cursor.getColumnIndex("address"))); int res = getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=?", new String[] { "" + cursor.getInt(cursor.getColumnIndex("_id")) }); Log.i("cky","geng xin = "+res); } } intent = new Intent(Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsApp); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); cursor = null; } } } }
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值