android - 从应用程序 A 发送广播并在应用程序 B 中接收

标签 android

我正在尝试将意向字符串 /Download/income_tax_return.pdf 作为广播从 App A 发送到 App B。在 App A 中,我有以下 MainActivity。

package com.mysender;


import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Intent intent=new Intent();
        intent.setAction("com.mysender.Data");
        intent.putExtra("path", "/Download/income_tax_return.pdf");
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        intent.setComponent(new ComponentName("com.example.app","com.example.app.MainActivity"));
        sendBroadcast(intent);

    }
}

初始化 Intent 后,我将 Intent 的操作设置如下:com.mysender.Data 并将 Intent 作为广播发送到应用程序 B com.example.app 但是当启动应用程序 A 然后启动应用程序 B 时,我不是 从 MyReceiver 获取 Toast 消息。如何将 Intent 从 App A 发送到 App B?我没有收到任何错误。

和下面的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mysender">
    <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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在 App B 中,我在 MainActivity 中执行以下操作:

p

ackage com.example.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Receive broad Cast fromn External App
        IntentFilter filter = new IntentFilter("com.mysender.Data");
        registerReceiver(myReceiver, filter);

    }
    private MyReceiver myReceiver =new MyReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            String path = intent.getStringExtra("path");
            Toast.makeText(context,"Data Received from External App: " + path, Toast.LENGTH_SHORT).show();

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);

    }
}

AndroidManifest.xml 应用 B:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.example.app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

编辑

所以我已经在第二个应用程序的 list 中注册了 MyReceiver,但我仍然没有得到路径字符串:

<receiver android:name=".MyReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.mysender.Data" />
    </intent-filter>
</receiver>

最佳答案

我认为您必须在您的第二个应用程序中注册 Intent 过滤器,否则它不会监听您发送的 Intent。

关于android - 从应用程序 A 发送广播并在应用程序 B 中接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43589295/

相关文章:

android - 模拟返回 Kotlin Coroutines Deferred 类型的方法的返回值

java - Android 基于 ListView /导航的应用程序

java - onpostexecute 中 Intent 的上下文不为空但出现空异常

java - 如何使用 Retrofit 在 post 请求中发送多个 json 对象?

Android 精确闹钟总是关闭 3 分钟

android - 如何在 Android WebView 中操作链接点击的 URL

android - FacebookMobile.init OAuthException

java - 有什么区别。在 default.properties 和 project.properties 之间?

javascript - 如何在 react-native 上为 android 和 ios 使用不同的样式?

android - 如何root后关闭其他应用程序?