Подтвердить что ты не робот

BroadcastReceiver не получает полное действие загрузки

Я пытаюсь захватить загружаемые полные события, но мой BroadcastReceiver не получает их. Вот приемник:

public class DownloadListenerService extends BroadcastReceiver {        
    @Override
    public void onReceive(final Context context, Intent intent) {
        System.out.println("got here");
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = settings.edit();

        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            String downloadPath = intent.getStringExtra(DownloadManager.COLUMN_URI);
            editor.putString("downloadPath", downloadPath);
            editor.commit();
        }
    }
}

Вот манифест:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    <receiver 
        android:name="com.example.alreadydownloaded.DownloadListenerService" 
        android:exported="true">
        <intent-filter>
            <action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
        </intent-filter>
    </receiver>
 </application>

Кто-нибудь видит, что неправильно?

4b9b3361

Ответ 1

  • Используйте полное имя пакета для получателя, например com.example.DownloadListenerService
  • Добавить android:exported="true" BroadcastReceiver может получать сообщения из источников outside своего приложения.
  • Измените имя Action в intent-filter на android.intent.action.DOWNLOAD_COMPLETE

        <receiver 
            android:name="com.example.DownloadListenerService"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
            </intent-filter>
        </receiver>
        <uses-permission android:name="android.permission.INTERNET" />
    

Приемник будет запущен только в том случае, если он был зарегистрирован в вашем приложении, используя registerReceiver(@Nullable BroadcastReceiver receiver,IntentFilter filter);

Код для включения Загрузить:

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://www.google.com/images/srpr/logo4w.png"));
dm.enqueue(request);

Ответ 2

Я думаю, что имя действия в вашем XML неверно. Документы утверждают, что правильный: android.intent.action.DOWNLOAD_COMPLETE не DownloadManager.ACTION_DOWNLOAD_COMPLETE - вам нужно использовать константу, а не форму Java.

<receiver android:name=".DownloadListenerService" >
    <intent-filter>
        <action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
    </intent-filter>
</receiver>

Ответ 3

  <receiver
        android:name=".BroadCast_Service.Download_BroadCast"
        android:exported="true">
        <intent-filter android:priority="1099">
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
        </intent-filter>
    </receiver>

Ответ 4

Я думаю, что вы вызываете службу DownloadManger из IntentService/Service. Если так удалить его оттуда и включить в действие.

добавить разрешение в манифесте android

 <uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />

Ответ 5

Если загрузка основана на вашем приложении, вам необходимо отправить трансляцию?

Intent i = new Intent();
i.setAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
sendBroadcast(i);