我的应用程序中有一项服务,客户应用程序可以通过向我的应用程序广播请求来运行。他们可以访问请求密钥。我想限制我的广播接收器只能接受来自某些包名的广播,以增加安全性。
我的服务(简化):
public class MyService extends Service {
private ResponseReceiver receiver;
@Override
public IBinder onBind(Intent intent) { return null; }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final String ACTION = "my_action";
// Register receiver
IntentFilter filter = new IntentFilter(ACTION);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new ResponseReceiver();
registerReceiver(receiver, filter);
return START_NOT_STICKY;
}
public class ResponseReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String REQUEST_KEY = "REQUEST";
String request = intent.getStringExtra(REQUEST_KEY);
Log.i("MainActivity", "Request from external application: " + request);
}
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
清单:
<service
android:name=".services.MyService"
android:enabled="true"
android:exported="true"/>
如何才能使其只有包名为com.my.application和com.customer.app的应用程序才能向我的应用发送广播?