首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >限制Android广播接收器使用特定的应用程序

限制Android广播接收器使用特定的应用程序
EN

Stack Overflow用户
提问于 2012-03-02 05:45:37
回答 2查看 14.9K关注 0票数 7

我有两份申请。

如果我使用服务,我可以设置权限,因此只有app1才能向app2发送意图。

app2 (protection level: signature)中定义权限,并在app1中使用该权限。

app2中的服务受该权限的保护。

这样,只有app1才能向app2上的服务发送意图,没有其他应用程序(除非我的签名被泄露)可以发送在app2上服务的意图。

我能用广播接收器做同样的事吗?

permission)

  • app2:
  • app1: sendBroadcast(意图,定义权限,使用该权限.

)

据我对使用sendBroadcast(意图、权限)的理解,应用程序不需要“使用”该权限。这意味着任何应用程序都可以向app2发送意图。这些权限参数仅针对app2进行检查,以避免其他应用程序接收此意图。(如果我删除app2,并安装具有相同权限字符串的假app2,则伪造app2可以从app1获得意图,这是出乎意料的)

顺便说一句,如果应用程序定义了权限并使用它本身,那么protectionLevel(签名)似乎没有任何意义。这是真的吗?

现在,我可以设置额外的权限:

permission.

  • app2:

  • app1:定义权限,只对该权限使用受限的

  • 接收器。

同样,如果删除app1,以完全相同的权限安装假app1,则假app1可以向app2发送假意图。我能做些什么来防止app2收到虚假的意图?

谢谢

EN

回答 2

Stack Overflow用户

发布于 2012-03-02 09:40:09

标记还可以定义广播公司应该拥有的权限,请参阅http://developer.android.com/guide/topics/manifest/receiver-element.html#prmsn

我的意思是,您可以通过这样的编码保护您的接收器免受未经授权的广播:

代码语言:javascript
复制
...
<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>
...
票数 6
EN

Stack Overflow用户

发布于 2022-02-09 10:17:45

这是我的代码:

发件人应用程序

MainActivity.java

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9528608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档