2

Issue: broadcasts android.intent.action.DOWNLOAD_COMPLETE is received only if application is running or in background. If application is killed then broadcast never received.

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>

Receiver class

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

Please note the I am facing this issue not in all devices.

Devices on which I am facing this issue: Lenevo A600, Asus Zenfone Max

Device on which it's working fine: Asus Zenfone 5 (cyanogenmod 13), Android Studio Emulator (Nexus 6p marshmallow), Samsung J7 Prime , Samsung j5, Nexus 5

Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77

3 Answers3

0

I think the reason may be due to the customization ROM ,limited the broadcast,CM,ASOP,is the originally System.so,,,

志威梦
  • 136
  • 3
0

if you want to receive broadcast even after app closed then you should use broadcast in service

following will help you to implement:- https://stackoverflow.com/a/16824692/4246910

Community
  • 1
  • 1
Amit Soni
  • 39
  • 6
  • I have created a service and made a broadcast in it but still it does not call onReceive method if app is killed. The service onReceive method invoked if app is running – Faisal Shaikh Jan 17 '17 at 17:33
0

The receiver appears to be an internal class. Since the app is destroyed so is your internal receiver class which is part of a destroyed class.

I am basing this assumption because your manifest has

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

Separate the 'BroadcastReceiver' into its own class and it should work as expected.

As for working on some phones, I would check the other apps running and what kind of app suppression apps you may have running in the background which may be contributing to force closing your app.

you should have something like this. i looked at code and you have nested receeiver but you are not holding the service on.

Calling function to start the action

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);
    }
}

in separate file here is the service

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();
    }
}

In separate file, here is the receiver

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();
    }
}
John Bravado
  • 137
  • 4
  • 10
  • made a different call for broadcast receiver but still no success. – Faisal Shaikh Jan 17 '17 at 17:34
  • check in your code. if the app is getting killed, then you are calling onStop(), onDestroy(). do you stop the receiver in any of those methods which could be causing this? – John Bravado Jan 17 '17 at 18:04
  • you make a good point. are you sure it is being sent a message. you still have not posted the code that says sendBrocast(intent). maybe when your app dies it is not the receiver that dies but the sender – John Bravado Jan 17 '17 at 18:31
  • I am not sending broadcast manually. The broadcast should be sent automatically if file is getting downloaded, that is why I have written in ~Manifest~ file. I have not called a such method sendBrocast(intent). – Faisal Shaikh Jan 17 '17 at 18:41
  • I believe, even if your receiver is declared in your manifest, you have to have a non killed app to run. If you force quit an app, the receivers also associated with that app and services i believe are destroyed. The only way to keep them alive, and i have only slightly tested this, is to create a startForeground service and register the receiver. but in doing so you will have to display a notification permanently – John Bravado Jan 17 '17 at 18:54
  • Can you send me some example for this – Faisal Shaikh Jan 17 '17 at 18:55
  • I was having same issue and tried many things. here is my question, i found out what my issue was but you can see my implementation of the service in order to get it to work. [Old Question](http://stackoverflow.com/questions/41662816/broadcastreceiver-dies-with-app) – John Bravado Jan 17 '17 at 19:07
  • I will try and let you know – Faisal Shaikh Jan 17 '17 at 19:08
  • I have created the service but its also not invoke service onReceive method – Faisal Shaikh Jan 17 '17 at 19:12
  • when you kill the app do you still have your notification from the running service? And you are no wrgistering the receiver in the service? – John Bravado Jan 17 '17 at 19:26
  • sorry sir, I am not getting you. – Faisal Shaikh Jan 18 '17 at 17:41
  • If you set up the service correctly you should have a notification displayed showing your service is running. And however you force quit, if the service remains, then the notification will remain. If you don't see a notification, then you are not running the service. – John Bravado Jan 18 '17 at 17:57
  • here is the code of service: http://paste.ofcode.org/EMYwB2SpQj5Pi5GFGEhr7u This service onReceive method invoked if app is running but does not invoked if is killed – Faisal Shaikh Jan 18 '17 at 18:00
  • I tried to copy and paste what i did and reduced it to bare minimums. please see my revised post. – John Bravado Jan 18 '17 at 19:11
  • I will try and let you know. – Faisal Shaikh Jan 18 '17 at 19:22