如果应用未运行,则不会收到广播DOWNLOAD_COMPLETE

时间:2017-01-16 19:18:33

标签: android android-intent android-broadcast android-download-manager android-broadcastreceiver

问题:广播仅在应用程序正在运行或在后台运行时才会收到android.intent.action.DOWNLOAD_COMPLETE。如果申请被杀,那么广播从未收到过。

的AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
<uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />

<receiver
    android:name=".adapters.VideoListAdapter$VideoDownloadedReceiver"
    android:exported="true">

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

接收者类

public static class VideoDownloadedReceiver extends BroadcastReceiver implements AsyncResponse {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("YES", "in receive");
    }
}

请注意我在所有设备中都没有遇到此问题。

我面临此问题的设备: Lenevo A600 华硕Zenfone Max

设备正常运行:华硕Zenfone 5(cyanogenmod 13),Android Studio模拟器(Nexus 6p marshmallow),三星J7 Prime,三星j5,Nexus 5

3 个答案:

答案 0 :(得分:0)

我认为原因可能是由于定制ROM,限制广播,CM,ASOP,原来是System.so ,,,

答案 1 :(得分:0)

如果您想在应用关闭后接收广播,那么您应该使用广播服务

以下将帮助您实施: - https://stackoverflow.com/a/16824692/4246910

答案 2 :(得分:0)

接收器似乎是内部类。由于应用程序被破坏,因此您的内部接收器类是被破坏的类的一部分。

我基于这个假设,因为你的清单有

android:name=".adapters.VideoListAdapter$VideoDownloadedReceiver"

将'BroadcastReceiver'分成它自己的类,它应该按预期工作。

至于在某些手机上工作,我会查看正在运行的其他应用以及您可能在后台运行哪种应用抑制应用,这可能有助于强制关闭您的应用。

你应该有类似的东西。我查看了代码,你已经嵌套了收件人,但你没有持有该服务。

调用函数以启动操作

private void startDownloadService() {
    Intent downloadIntent = new Intent(this, VideoDownloadReceiverService.class);

    if (!VideoDownloadReceiverService.isRunning()) {
        // My manifest has enabled="false" so i do this to turn it 'ON'
        component = new ComponentName(CounterActivity.this, VideoDownloadReceiverService.class);
        getPackageManager()
                .setComponentEnabledSetting(component,
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
        // Start the service
        startService(downloadIntent);
    }
}

在单独的文件中是服务

public class VideoDownloadReceiverService extends Service{

    private static boolean isRunning = false;

    public VideoDownloadReceiverService(){

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        // you may not need this since oyu register it in the Manifest. the fact the service is running
        // should keep your app alive and receiving and unable to be shutdown
        // but this is what i did
        IntentFilter filter = new IntentFilter();
        filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        receiver = new VideoDownloadReceiver();
        registerReceiver(receiver, filter);

        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentText("Viewer Text In Notification").build();
        startForeground(MyConstants.NOTIFICATION_ID, notification);

        isRunning = true;
        return START_STICKY; // This is the line that keeps the service running no matter what
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy(){
        unregisterReceiver(receiver);
        super.onDestroy();
    }
}

在单独的文件中,这是接收器

public class VideoDownloadReceiver extends BroadcastReceiver {
    public VideoDownloadReceiver() {

    }
    @Override
    public void onReceive(Context context, Intent intent) {
        PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "com.example.johnbravado.zionwork");
        wakeLock.acquire();

        Toast.makeText(context, "in service", Toast.LENGTH_SHORT).show();
        Log.i("YES", "service on receive");

        wakeLock.release();
    }
}