我有两份申请。
如果我使用服务,我可以设置权限,因此只有app1才能向app2发送意图。
在app2 (protection level: signature)中定义权限,并在app1中使用该权限。
app2中的服务受该权限的保护。
这样,只有app1才能向app2上的服务发送意图,没有其他应用程序(除非我的签名被泄露)可以发送在app2上服务的意图。
我能用广播接收器做同样的事吗?
permission)
)
据我对使用sendBroadcast(意图、权限)的理解,应用程序不需要“使用”该权限。这意味着任何应用程序都可以向app2发送意图。这些权限参数仅针对app2进行检查,以避免其他应用程序接收此意图。(如果我删除app2,并安装具有相同权限字符串的假app2,则伪造app2可以从app1获得意图,这是出乎意料的)
顺便说一句,如果应用程序定义了权限并使用它本身,那么protectionLevel(签名)似乎没有任何意义。这是真的吗?
现在,我可以设置额外的权限:
permission.
同样,如果删除app1,以完全相同的权限安装假app1,则假app1可以向app2发送假意图。我能做些什么来防止app2收到虚假的意图?
谢谢
发布于 2012-03-02 09:40:09
标记还可以定义广播公司应该拥有的权限,请参阅http://developer.android.com/guide/topics/manifest/receiver-element.html#prmsn。
我的意思是,您可以通过这样的编码保护您的接收器免受未经授权的广播:
...
<permission android:name="com.yourapp.PERMISSION"
android:protectionLevel="signature"
android:label="@string/permission_label"
android:description="@string/permission_desc">
</permission>
...
<receiver android:name=".MyReceiver"
android:permission="com.yourapp.PERMISSION">
<intent-filter>
<action android:name="com.yourapp.ACTION" />
</intent-filter>
</receiver>
...发布于 2022-02-09 10:17:45
这是我的代码:
发件人应用程序
MainActivity.java
package com.karthik.apptx;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void OnClickButton(View view) {
Intent intent = new Intent();
intent.setAction("com.karthik.apprx");
intent.putExtra("PACKAGE_NAME", this.getPackageName());
intent.putExtra("PID", Process.myPid());
intent.putExtra("DATA", "Hello Karthik");
intent.setComponent(new ComponentName("com.karthik.apprx", "com.karthik.apprx.AppRxReceiver"));
sendBroadcast(intent);
}
}AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.karthik.apptx">
<uses-permission android:name="com.karthik.PERMISSION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppTx">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>接收机应用程序(用Kotlin编写)
MyBroadcastReceiver.kt
package com.karthik.apprx
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
class AppRxReceiver : BroadcastReceiver() {
override fun onReceive(ctx: Context?, intent: Intent?) {
Log.d("AppRxReceiver", intent.toString())
Log.d("AppRxReceiver", intent?.getStringExtra("PACKAGE_NAME").toString())
Log.d("AppRxReceiver", "" + intent?.getIntExtra("PID", 0))
Log.d("AppRxReceiver", intent?.getStringExtra("DATA").toString())
Toast.makeText(ctx, "Received Data", Toast.LENGTH_SHORT).show()
}
}AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.karthik.apprx">
<permission
android:name="com.karthik.PERMISSION"
android:protectionLevel="signature"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppRx">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.AppRx.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AppRxReceiver"
android:enabled="true"
android:exported="true"
android:permission="com.karthik.PERMISSION">
<intent-filter>
<action android:name="com.karthik.apptx" />
</intent-filter>
</receiver>
</application>
</manifest>PS:为了方便起见,我选择用Java编写Sender,用Kotlin编写Receiver。
https://stackoverflow.com/questions/9528608
复制相似问题