android - 无法向关闭的应用程序发送广播

标签 android android-intent android-broadcast android-lvl

我们开发了一个应用程序,需要第二个应用程序(专业 key 应用程序)来验证其许可证。主要(免费应用程序)通过广播向专业 key 应用程序请求检查许可证。 问题是,当专业 key 应用程序关闭时,它永远不会收到主应用程序发送的广播,要解决此问题,我必须先打开专业 key 应用程序,然后再次尝试验证许可证。

以下是主应用程序发送它的方式:

public static void checkLicense(Context context) {
    ...
    Intent checkLicenseIntent = new Intent(Constants.CHECK_LICENSE_INTENT);
    context.sendBroadcast(checkLicenseIntent);
    ...
}

这是专业 key 应用程序的 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ar.com.myapp.prokey"
    android:versionCode="2"
    android:versionName="1.0.0.1" >

    <permission
        android:name="ar.com.myapp.prokey.CheckLicense"
        android:protectionLevel="signature" />

    <uses-permission android:name="ar.com.myapp.ReceiveLicenseCheckResponse" />
    <uses-permission android:name="com.android.vending.CHECK_LICENSE" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="ar.com.myapp.prokey.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>

        <receiver
            android:name="ar.com.myapp.prokey.CheckLicenseReceiver"
            android:permission="ar.com.myapp.prokey.CheckLicense" >
            <intent-filter >
                <action android:name="ar.com.myapp.prokey.CHECK_LICENSE" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
        </receiver>

        <service android:name="ar.com.myapp.prokey.LicenseVerificationService" />
    </application>

</manifest>

这是应用程序通信的方式吗?广播是否应该唤醒关键专业应用程序? 有什么想法吗?

最佳答案

如果您的应用处于“已停止”状态,它将无法接收任何 Intent 。您需要通过激活应用程序的任何组件来“激活”该应用程序。如果您对广播的 Intent 使用 setComponent(),系统将在发送 Intent 之前激活应用程序。

将此行添加到您的代码中:

Intent checkLicenseIntent = new Intent(Constants.CHECK_LICENSE_INTENT);
checkLicenseIntent.setComponent(new ComponentName("ar.com.myapp.prokey", "ar.com.myapp.prokey.CheckLicenseReceiver"));
context.sendBroadcast(checkLicenseIntent);

您可以阅读这篇文章的更多内容:https://devmaze.wordpress.com/2011/12/05/activating-applications/

关于android - 无法向关闭的应用程序发送广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28842043/

相关文章:

android - 日期/时间选择器 flutter : OK/CANCEL button not visible

android - 图书馆项目中的广播接收器无法正常工作

java - 如何触发扩展 BroadcastReceiver 的类

java - 如何使用广播接收器在android和另一个进程之间进行通信

android - Realm Android : what is mean by isValid(), isLoaded(), isManaged()?

android - 如何同时使用 Android 电子市场和 SlideMe?

java - 返回值 android 中 Intent 的最佳实践

Android 和 Facebook 共享 Intent

android - getIntent().getExtras() 在 API 级别 21 中有时为 null?

android - 如何使用 Firebase 在 Android 中解决 Deep Link Enable